home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / emacs / site-lisp / dictionaries-common / flyspell.el < prev    next >
Encoding:
Text File  |  2009-02-20  |  96.0 KB  |  2,404 lines

  1. ;;; flyspell.el --- on-the-fly spell checker
  2.  
  3. ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,
  4. ;;   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
  5.  
  6. ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.fr>
  7. ;; Maintainer: FSF
  8. ;; Keywords: convenience
  9.  
  10. ;; This file is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
  24.  
  25. ;;; Commentary:
  26. ;;
  27. ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
  28. ;; checking.
  29. ;;
  30. ;; To enable Flyspell minor mode, type M-x flyspell-mode.
  31. ;; This applies only to the current buffer.
  32. ;;
  33. ;; To enable Flyspell in text representing computer programs, type
  34. ;; M-x flyspell-prog-mode.
  35. ;; In that mode only text inside comments is checked.
  36. ;;
  37. ;; Note: consider setting the variable ispell-parser to `tex' to
  38. ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
  39. ;;
  40. ;; Some user variables control the behavior of flyspell.  They are
  41. ;; those defined under the `User variables' comment.
  42.  
  43. ;; --------------------------------------------------------------------
  44. ;; This file has been modified by Agustin Martin <agmartin@debian.org>
  45. ;; for the Debian GNU/Linux system to meet the requirements of the Debian
  46. ;; Spelling Policy, add capabilities and fix bugs
  47. ;;
  48. ;; This file is included in the Debian dictionaries-common package
  49. ;;
  50. ;; We define flyspell-version function and var to let people know about this
  51.  
  52. (defun flyspell-version ()
  53.   "The flyspell version"
  54.   (interactive)
  55.   "FSF emacs CVS20090116(r1.141) + Debian `dictionaries-common' changes (like xemacs compatibility)")
  56.  
  57. ;; ispell.el provides a var for this. Provide one too.
  58. (defvar flyspell-version (flyspell-version)
  59.   "The flyspell version")
  60.  
  61. ;; --------------------------------------------------------------------
  62.  
  63. ;;; Code:
  64.  
  65. (require 'ispell)
  66.  
  67. ;;*---------------------------------------------------------------------*/
  68. ;;*    Group ...                                                        */
  69. ;;*---------------------------------------------------------------------*/
  70. (defgroup flyspell nil
  71.   "Spell checking on the fly."
  72.   :tag "FlySpell"
  73.   :prefix "flyspell-"
  74.   :group 'ispell
  75.   :group 'processes)
  76.  
  77. ;;*---------------------------------------------------------------------*/
  78. ;;*    User configuration ...                                           */
  79. ;;*---------------------------------------------------------------------*/
  80. (defcustom flyspell-highlight-flag t
  81.   "How Flyspell should indicate misspelled words.
  82. Non-nil means use highlight, nil means use minibuffer messages."
  83.   :group 'flyspell
  84.   :type 'boolean)
  85.  
  86. (defcustom flyspell-mark-duplications-flag t
  87.   "Non-nil means Flyspell reports a repeated word as an error.
  88. See `flyspell-mark-duplications-exceptions' to add exceptions to this rule.
  89. Detection of repeated words is not implemented in
  90. \"large\" regions; see `flyspell-large-region'."
  91.   :group 'flyspell
  92.   :type 'boolean)
  93.  
  94. (defcustom flyspell-mark-duplications-exceptions
  95.   '(("francais" . ("nous" "vous")))
  96.   "A list of exceptions for duplicated words.
  97. It should be a list of (LANGUAGE . EXCEPTION-LIST).  LANGUAGE is matched
  98. against the current dictionary and EXCEPTION-LIST is a list of strings.
  99. The duplicated word is downcased before it is compared with the exceptions."
  100.   :group 'flyspell
  101.   :type '(alist :key-type string :value-type (repeat string)))
  102.  
  103. (defcustom flyspell-sort-corrections nil
  104.   "Non-nil means, sort the corrections alphabetically before popping them."
  105.   :group 'flyspell
  106.   :version "21.1"
  107.   :type 'boolean)
  108.  
  109. (defcustom flyspell-duplicate-distance -1
  110.   "The maximum distance for finding duplicates of unrecognized words.
  111. This applies to the feature that when a word is not found in the dictionary,
  112. if the same spelling occurs elsewhere in the buffer,
  113. Flyspell uses a different face (`flyspell-duplicate') to highlight it.
  114. This variable specifies how far to search to find such a duplicate.
  115. -1 means no limit (search the whole buffer).
  116. 0 means do not search for duplicate unrecognized spellings."
  117.   :group 'flyspell
  118.   :version "21.1"
  119.   :type '(choice (const :tag "no limit" -1)
  120.          number))
  121.  
  122. (defcustom flyspell-delay 3
  123.   "The number of seconds to wait before checking, after a \"delayed\" command."
  124.   :group 'flyspell
  125.   :type 'number)
  126.  
  127. (defcustom flyspell-persistent-highlight t
  128.   "Non-nil means misspelled words remain highlighted until corrected.
  129. If this variable is nil, only the most recently detected misspelled word
  130. is highlighted."
  131.   :group 'flyspell
  132.   :type 'boolean)
  133.  
  134. (defcustom flyspell-highlight-properties t
  135.   "Non-nil means highlight incorrect words even if a property exists for this word."
  136.   :group 'flyspell
  137.   :type 'boolean)
  138.  
  139. (defcustom flyspell-default-delayed-commands
  140.   '(self-insert-command
  141.     delete-backward-char
  142.     backward-or-forward-delete-char
  143.     delete-char
  144.     scrollbar-vertical-drag
  145.     backward-delete-char-untabify)
  146.   "The standard list of delayed commands for Flyspell.
  147. See `flyspell-delayed-commands'."
  148.   :group 'flyspell
  149.   :version "21.1"
  150.   :type '(repeat (symbol)))
  151.  
  152. (defcustom flyspell-delayed-commands nil
  153.   "List of commands that are \"delayed\" for Flyspell mode.
  154. After these commands, Flyspell checking is delayed for a short time,
  155. whose length is specified by `flyspell-delay'."
  156.   :group 'flyspell
  157.   :type '(repeat (symbol)))
  158.  
  159. (defcustom flyspell-default-deplacement-commands
  160.   '(next-line
  161.     previous-line
  162.     scroll-up
  163.     scroll-down)
  164.   "The standard list of deplacement commands for Flyspell.
  165. See `flyspell-deplacement-commands'."
  166.   :group 'flyspell
  167.   :version "21.1"
  168.   :type '(repeat (symbol)))
  169.  
  170. (defcustom flyspell-deplacement-commands nil
  171.   "List of commands that are \"deplacement\" for Flyspell mode.
  172. After these commands, Flyspell checking is performed only if the previous
  173. command was not the very same command."
  174.   :group 'flyspell
  175.   :version "21.1"
  176.   :type '(repeat (symbol)))
  177.  
  178. (defcustom flyspell-issue-welcome-flag t
  179.   "Non-nil means that Flyspell should display a welcome message when started."
  180.   :group 'flyspell
  181.   :type 'boolean)
  182.  
  183. (defcustom flyspell-issue-message-flag t
  184.   "Non-nil means that Flyspell emits messages when checking words."
  185.   :group 'flyspell
  186.   :type 'boolean)
  187.  
  188. (defcustom flyspell-incorrect-hook nil
  189.   "List of functions to be called when incorrect words are encountered.
  190. Each function is given three arguments.  The first two
  191. arguments are the beginning and the end of the incorrect region.
  192. The third is either the symbol `doublon' or the list
  193. of possible corrections as returned by `ispell-parse-output'.
  194.  
  195. If any of the functions return non-nil, the word is not highlighted as
  196. incorrect."
  197.   :group 'flyspell
  198.   :version "21.1"
  199.   :type 'hook)
  200.  
  201. (defcustom flyspell-default-dictionary nil
  202.   "A string that is the name of the default dictionary.
  203. This is passed to the `ispell-change-dictionary' when flyspell is started.
  204. If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
  205. when flyspell is started, the value of that variable is used instead
  206. of `flyspell-default-dictionary' to select the default dictionary.
  207. Otherwise, if `flyspell-default-dictionary' is nil, it means to use
  208. Ispell's ultimate default dictionary."
  209.   :group 'flyspell
  210.   :version "21.1"
  211.   :type '(choice string (const :tag "Default" nil)))
  212.  
  213. (defcustom flyspell-tex-command-regexp
  214.   "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
  215.   "A string that is the regular expression that matches TeX commands."
  216.   :group 'flyspell
  217.   :version "21.1"
  218.   :type 'string)
  219.  
  220. (defcustom flyspell-check-tex-math-command nil
  221.   "Non-nil means check even inside TeX math environment.
  222. TeX math environments are discovered by the TEXMATHP that implemented
  223. inside the texmathp.el Emacs package.  That package may be found at:
  224. http://strw.leidenuniv.nl/~dominik/Tools"
  225.   :group 'flyspell
  226.   :type 'boolean)
  227.  
  228. (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
  229.   '("francais" "deutsch8" "norsk")
  230.   "List of dictionary names that consider `-' as word delimiter."
  231.   :group 'flyspell
  232.   :version "21.1"
  233.   :type '(repeat (string)))
  234.  
  235. (defcustom flyspell-abbrev-p
  236.   nil
  237.   "If non-nil, add correction to abbreviation table."
  238.   :group 'flyspell
  239.   :version "21.1"
  240.   :type 'boolean)
  241.  
  242. (defcustom flyspell-use-global-abbrev-table-p
  243.   nil
  244.   "If non-nil, prefer global abbrev table to local abbrev table."
  245.   :group 'flyspell
  246.   :version "21.1"
  247.   :type 'boolean)
  248.  
  249. (defcustom flyspell-mode-line-string " Fly"
  250.   "String displayed on the modeline when flyspell is active.
  251. Set this to nil if you don't want a modeline indicator."
  252.   :group 'flyspell
  253.   :type '(choice string (const :tag "None" nil)))
  254.  
  255. (defcustom flyspell-large-region 1000
  256.   "The threshold that determines if a region is small.
  257. If the region is smaller than this number of characters,
  258. `flyspell-region' checks the words sequentially using regular
  259. flyspell methods.  Else, if the region is large, a new Ispell process is
  260. spawned for speed.
  261.  
  262. Doubled words are not detected in a large region, because Ispell
  263. does not check for them.
  264.  
  265. If `flyspell-large-region' is nil, all regions are treated as small."
  266.   :group 'flyspell
  267.   :version "21.1"
  268.   :type '(choice number (const :tag "All small" nil)))
  269.  
  270. (defcustom flyspell-insert-function (function insert)
  271.   "Function for inserting word by flyspell upon correction."
  272.   :group 'flyspell
  273.   :type 'function)
  274.  
  275. (defcustom flyspell-before-incorrect-word-string nil
  276.   "String used to indicate an incorrect word starting."
  277.   :group 'flyspell
  278.   :type '(choice string (const nil)))
  279.  
  280. (defcustom flyspell-after-incorrect-word-string nil
  281.   "String used to indicate an incorrect word ending."
  282.   :group 'flyspell
  283.   :type '(choice string (const nil)))
  284.  
  285. (defcustom flyspell-use-meta-tab t
  286.   "Non-nil means that flyspell uses M-TAB to correct word."
  287.   :group 'flyspell
  288.   :type 'boolean)
  289.  
  290. (defcustom flyspell-auto-correct-binding
  291.   [(control ?\;)]
  292.   "The key binding for flyspell auto correction."
  293.   :group 'flyspell)
  294.  
  295. ;;*---------------------------------------------------------------------*/
  296. ;;*    Mode specific options                                            */
  297. ;;*    -------------------------------------------------------------    */
  298. ;;*    Mode specific options enable users to disable flyspell on        */
  299. ;;*    certain word depending of the emacs mode. For instance, when     */
  300. ;;*    using flyspell with mail-mode add the following expression       */
  301. ;;*    in your .emacs file:                                             */
  302. ;;*       (add-hook 'mail-mode                                          */
  303. ;;*             '(lambda () (setq flyspell-generic-check-word-predicate    */
  304. ;;*                       'mail-mode-flyspell-verify)))            */
  305. ;;*---------------------------------------------------------------------*/
  306. (defvar flyspell-generic-check-word-predicate nil
  307.   "Function providing per-mode customization over which words are flyspelled.
  308. Returns t to continue checking, nil otherwise.
  309. Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
  310. property of the major mode name.")
  311. (make-variable-buffer-local 'flyspell-generic-check-word-predicate)
  312. (if (fboundp 'defvaralias)
  313.     (defvaralias 'flyspell-generic-check-word-p
  314.       'flyspell-generic-check-word-predicate))
  315.  
  316. ;;*--- mail mode -------------------------------------------------------*/
  317. (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
  318. (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
  319. (defvar message-signature-separator)
  320. (defun mail-mode-flyspell-verify ()
  321.   "Function used for `flyspell-generic-check-word-predicate' in Mail mode."
  322.   (let ((header-end (save-excursion
  323.               (goto-char (point-min))
  324.               (re-search-forward
  325.                (concat "^"
  326.                    (regexp-quote mail-header-separator)
  327.                    "$")
  328.                nil t)
  329.               (point)))
  330.     (signature-begin
  331.          (if (not (boundp 'message-signature-separator))
  332.              (point-max)
  333.            (save-excursion
  334.              (goto-char (point-max))
  335.              (re-search-backward message-signature-separator nil t)
  336.              (point)))))
  337.     (cond ((< (point) header-end)
  338.        (and (save-excursion (beginning-of-line)
  339.                 (looking-at "^Subject:"))
  340.         (> (point) (match-end 0))))
  341.       ((> (point) signature-begin)
  342.        nil)
  343.       (t
  344.        (save-excursion
  345.          (beginning-of-line)
  346.          (not (looking-at "[>}|]\\|To:")))))))
  347.  
  348. ;;*--- texinfo mode ----------------------------------------------------*/
  349. (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
  350. (defun texinfo-mode-flyspell-verify ()
  351.   "Function used for `flyspell-generic-check-word-predicate' in Texinfo mode."
  352.   (save-excursion
  353.     (forward-word -1)
  354.     (not (looking-at "@"))))
  355.  
  356. ;;*--- tex mode --------------------------------------------------------*/
  357. (put 'tex-mode 'flyspell-mode-predicate 'tex-mode-flyspell-verify)
  358. (defun tex-mode-flyspell-verify ()
  359.   "Function used for `flyspell-generic-check-word-predicate' in LaTeX mode."
  360.   (and
  361.    (not (save-excursion
  362.       (re-search-backward "^[ \t]*%%%[ \t]+Local" nil t)))
  363.    (not (save-excursion
  364.       (let ((this (point-marker))
  365.         (e (progn (end-of-line) (point-marker))))
  366.         (beginning-of-line)
  367.         (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t)
  368.         (and (>= this (match-beginning 0))
  369.              (<= this (match-end 0)) )))))))
  370.  
  371. ;;*--- sgml mode -------------------------------------------------------*/
  372. (put 'sgml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
  373. (put 'html-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
  374. (put 'nxml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
  375.  
  376. (defun sgml-mode-flyspell-verify ()
  377.   "Function used for `flyspell-generic-check-word-predicate' in SGML mode."
  378.   (not (save-excursion
  379.      (let ((this (point-marker))
  380.            (s (progn (beginning-of-line) (point-marker)))
  381.            (e (progn (end-of-line) (point-marker))))
  382.        (or (progn
  383.          (goto-char this)
  384.          (and (re-search-forward  "[^<]*>" e t)
  385.               (= (match-beginning 0) this)))
  386.            (progn
  387.          (goto-char this)
  388.          (and (re-search-backward "<[^>]*" s t)
  389.               (= (match-end 0) this)))
  390.            (and (progn
  391.               (goto-char this)
  392.               (and (re-search-forward  "[^&]*;" e t)
  393.                (= (match-beginning 0) this)))
  394.             (progn
  395.               (goto-char this)
  396.               (and (re-search-backward "&[^;]*" s t)
  397.                (= (match-end 0) this)))))))))
  398.  
  399. ;;*---------------------------------------------------------------------*/
  400. ;;*    Programming mode                                                 */
  401. ;;*---------------------------------------------------------------------*/
  402. (defvar flyspell-prog-text-faces
  403.   '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
  404.   "Faces corresponding to text in programming-mode buffers.")
  405.  
  406. (defun flyspell-generic-progmode-verify ()
  407.   "Used for `flyspell-generic-check-word-predicate' in programming modes."
  408.   (let ((f (get-text-property (point) 'face)))
  409.     (memq f flyspell-prog-text-faces)))
  410.  
  411. ;;;###autoload
  412. (defun flyspell-prog-mode ()
  413.   "Turn on `flyspell-mode' for comments and strings."
  414.   (interactive)
  415.   (setq flyspell-generic-check-word-predicate
  416.         'flyspell-generic-progmode-verify)
  417.   (flyspell-mode 1)
  418.   (run-hooks 'flyspell-prog-mode-hook))
  419.  
  420. ;;*---------------------------------------------------------------------*/
  421. ;;*    Overlay compatibility                                            */
  422. ;;*---------------------------------------------------------------------*/
  423. (autoload 'make-overlay            "overlay" "Overlay compatibility kit." t)
  424. (autoload 'overlayp                "overlay" "Overlay compatibility kit." t)
  425. (autoload 'overlays-in             "overlay" "Overlay compatibility kit." t)
  426. (autoload 'delete-overlay          "overlay" "Overlay compatibility kit." t)
  427. (autoload 'overlays-at             "overlay" "Overlay compatibility kit." t)
  428. (autoload 'overlay-put             "overlay" "Overlay compatibility kit." t)
  429. (autoload 'overlay-get             "overlay" "Overlay compatibility kit." t)
  430. (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
  431.  
  432. ;;*---------------------------------------------------------------------*/
  433. ;;*    The minor mode declaration.                                      */
  434. ;;*---------------------------------------------------------------------*/
  435. (defvar flyspell-mouse-map
  436.   (let ((map (make-sparse-keymap)))
  437.     (define-key map (if (featurep 'xemacs) [button2] [down-mouse-2])
  438.       #'flyspell-correct-word)
  439.     map)
  440.   "Keymap for Flyspell to put on erroneous words.")
  441.  
  442. (defvar flyspell-mode-map
  443.   (let ((map (make-sparse-keymap)))
  444.     (if flyspell-use-meta-tab
  445.       (define-key map "\M-\t" 'flyspell-auto-correct-word))
  446.     (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
  447.     (define-key map [(control ?\,)] 'flyspell-goto-next-error)
  448.     (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
  449.     (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
  450.     map)
  451.   "Minor mode keymap for Flyspell mode--for the whole buffer.")
  452.  
  453. ;; dash character machinery
  454. (defvar flyspell-consider-dash-as-word-delimiter-flag nil
  455.    "*Non-nil means that the `-' char is considered as a word delimiter.")
  456. (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
  457. (defvar flyspell-dash-dictionary nil)
  458. (make-variable-buffer-local 'flyspell-dash-dictionary)
  459. (defvar flyspell-dash-local-dictionary nil)
  460. (make-variable-buffer-local 'flyspell-dash-local-dictionary)
  461.  
  462. ;;*---------------------------------------------------------------------*/
  463. ;;*    Highlighting                                                     */
  464. ;;*---------------------------------------------------------------------*/
  465. (defface flyspell-incorrect
  466.   '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
  467.     (t (:bold t)))
  468.   "Face used for marking a misspelled word in Flyspell."
  469.   :group 'flyspell)
  470. ;; backward-compatibility alias
  471. (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
  472.  
  473. (defface flyspell-duplicate
  474.   '((((class color)) (:foreground "Gold3" :bold t :underline t))
  475.     (t (:bold t)))
  476.   "Face used for marking a misspelled word that appears twice in the buffer.
  477. See also `flyspell-duplicate-distance'."
  478.   :group 'flyspell)
  479. ;; backward-compatibility alias
  480. (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
  481.  
  482. (defvar flyspell-overlay nil)
  483.  
  484. ;;*---------------------------------------------------------------------*/
  485. ;;*    flyspell-mode ...                                                */
  486. ;;*---------------------------------------------------------------------*/
  487. ;;;###autoload(defvar flyspell-mode nil)
  488. ;;;###autoload
  489. (define-minor-mode flyspell-mode
  490.   "Minor mode performing on-the-fly spelling checking.
  491. This spawns a single Ispell process and checks each word.
  492. The default flyspell behavior is to highlight incorrect words.
  493. With no argument, this command toggles Flyspell mode.
  494. With a prefix argument ARG, turn Flyspell minor mode on if ARG is positive,
  495. otherwise turn it off.
  496.  
  497. Bindings:
  498. \\[ispell-word]: correct words (using Ispell).
  499. \\[flyspell-auto-correct-word]: automatically correct word.
  500. \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
  501. \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
  502.  
  503. Hooks:
  504. This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
  505.  
  506. Remark:
  507. `flyspell-mode' uses `ispell-mode'.  Thus all Ispell options are
  508. valid.  For instance, a different dictionary can be used by
  509. invoking `ispell-change-dictionary'.
  510.  
  511. Consider using the `ispell-parser' to check your text.  For instance
  512. consider adding:
  513. \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
  514. in your .emacs file.
  515.  
  516. \\[flyspell-region] checks all words inside a region.
  517. \\[flyspell-buffer] checks the whole buffer."
  518.   :lighter flyspell-mode-line-string
  519.   :keymap flyspell-mode-map
  520.   :group 'flyspell
  521.   (if flyspell-mode
  522.       (condition-case ()
  523.       (flyspell-mode-on)
  524.     (error (message "Enabling Flyspell mode gave an error")
  525.            (flyspell-mode -1)))
  526.     (flyspell-mode-off)))
  527.  
  528. ;;;###autoload
  529. (defun turn-on-flyspell ()
  530.   "Unconditionally turn on Flyspell mode."
  531.   (flyspell-mode 1))
  532.  
  533. ;;;###autoload
  534. (defun turn-off-flyspell ()
  535.   "Unconditionally turn off Flyspell mode."
  536.   (flyspell-mode -1))
  537.  
  538. (custom-add-option 'text-mode-hook 'turn-on-flyspell)
  539.  
  540. ;;*---------------------------------------------------------------------*/
  541. ;;*    flyspell-buffers ...                                             */
  542. ;;*    -------------------------------------------------------------    */
  543. ;;*    For remembering buffers running flyspell                         */
  544. ;;*---------------------------------------------------------------------*/
  545. (defvar flyspell-buffers nil)
  546.  
  547. ;;*---------------------------------------------------------------------*/
  548. ;;*    flyspell-minibuffer-p ...                                        */
  549. ;;*---------------------------------------------------------------------*/
  550. (defun flyspell-minibuffer-p (buffer)
  551.   "Is BUFFER a minibuffer?"
  552.   (let ((ws (get-buffer-window-list buffer t)))
  553.     (and (consp ws) (window-minibuffer-p (car ws)))))
  554.  
  555. ;;*---------------------------------------------------------------------*/
  556. ;;*    flyspell-accept-buffer-local-defs ...                            */
  557. ;;*---------------------------------------------------------------------*/
  558. (defvar flyspell-last-buffer nil
  559.   "The buffer in which the last flyspell operation took place.")
  560.  
  561. (defun flyspell-accept-buffer-local-defs (&optional force)
  562.   ;; When flyspell-word is used inside a loop (e.g. when processing
  563.   ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
  564.   ;; up dwarfing everything else, so only do it when the buffer has changed.
  565.   (when (or force (not (eq flyspell-last-buffer (current-buffer))))
  566.     (setq flyspell-last-buffer (current-buffer))
  567.     ;; Strange problem:  If buffer in current window has font-lock turned on,
  568.     ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
  569.     ;; call will reset the buffer to the buffer in the current window.
  570.     ;; However, it only happens at startup (fix by Albert L. Ting).
  571.     (save-current-buffer
  572.       (ispell-accept-buffer-local-defs))
  573.     (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
  574.                  (eq flyspell-dash-local-dictionary ispell-local-dictionary))
  575.       ;; The dictionary has changed
  576.       (setq flyspell-dash-dictionary ispell-dictionary)
  577.       (setq flyspell-dash-local-dictionary ispell-local-dictionary)
  578.       (setq flyspell-consider-dash-as-word-delimiter-flag
  579.             (member (or ispell-local-dictionary ispell-dictionary)
  580.                     flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
  581.  
  582. (defun flyspell-hack-local-variables-hook ()
  583.   ;; When local variables are loaded, see if the dictionary context
  584.   ;; has changed.
  585.   (flyspell-accept-buffer-local-defs 'force))
  586.  
  587. (defun flyspell-kill-ispell-hook ()
  588.   (setq flyspell-last-buffer nil)
  589.   (dolist (buf (buffer-list))
  590.     (with-current-buffer buf
  591.       (kill-local-variable 'flyspell-word-cache-word))))
  592.  
  593. ;; Make sure we flush our caches when needed.  Do it here rather than in
  594. ;; flyspell-mode-on, since flyspell-region may be used without ever turning
  595. ;; on flyspell-mode.
  596. (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
  597.  
  598. ;;*---------------------------------------------------------------------*/
  599. ;;*    flyspell-mode-on ...                                             */
  600. ;;*---------------------------------------------------------------------*/
  601. (defun flyspell-mode-on ()
  602.   "Turn Flyspell mode on.  Do not use this; use `flyspell-mode' instead."
  603.   (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
  604.   (setq ispell-highlight-face 'flyspell-incorrect)
  605.   ;; local dictionaries setup
  606.   (or ispell-local-dictionary ispell-dictionary
  607.       (if flyspell-default-dictionary
  608.       (ispell-change-dictionary flyspell-default-dictionary)))
  609.   ;; we have to force ispell to accept the local definition or
  610.   ;; otherwise it could be too late, the local dictionary may
  611.   ;; be forgotten!
  612.   ;; Pass the `force' argument for the case where flyspell was active already
  613.   ;; but the buffer's local-defs have been edited.
  614.   (flyspell-accept-buffer-local-defs 'force)
  615.   ;; we put the `flyspell-delayed' property on some commands
  616.   (flyspell-delay-commands)
  617.   ;; we put the `flyspell-deplacement' property on some commands
  618.   (flyspell-deplacement-commands)
  619.   ;; we bound flyspell action to post-command hook
  620.   (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
  621.   ;; we bound flyspell action to pre-command hook
  622.   (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
  623.   ;; we bound flyspell action to after-change hook
  624.   (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
  625.   ;; we bound flyspell action to hack-local-variables-hook
  626.   (add-hook 'hack-local-variables-hook
  627.         (function flyspell-hack-local-variables-hook) t t)
  628.   ;; set flyspell-generic-check-word-predicate based on the major mode
  629.   (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
  630.     (if mode-predicate
  631.     (setq flyspell-generic-check-word-predicate mode-predicate)))
  632.   ;; the welcome message
  633.   (if (and flyspell-issue-message-flag
  634.        flyspell-issue-welcome-flag
  635.        (interactive-p))
  636.       (let ((binding (where-is-internal 'flyspell-auto-correct-word
  637.                     nil 'non-ascii)))
  638.     (message "%s"
  639.      (if binding
  640.          (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
  641.              (key-description binding))
  642.        "Welcome to flyspell. Use Mouse-2 to correct words.")))))
  643.  
  644. ;;*---------------------------------------------------------------------*/
  645. ;;*    flyspell-delay-commands ...                                      */
  646. ;;*---------------------------------------------------------------------*/
  647. (defun flyspell-delay-commands ()
  648.   "Install the standard set of Flyspell delayed commands."
  649.   (mapc 'flyspell-delay-command flyspell-default-delayed-commands)
  650.   (mapcar 'flyspell-delay-command flyspell-delayed-commands))
  651.  
  652. ;;*---------------------------------------------------------------------*/
  653. ;;*    flyspell-delay-command ...                                       */
  654. ;;*---------------------------------------------------------------------*/
  655. (defun flyspell-delay-command (command)
  656.   "Set COMMAND to be delayed, for Flyspell.
  657. When flyspell `post-command-hook' is invoked because a delayed command
  658. as been used the current word is not immediately checked.
  659. It will be checked only after `flyspell-delay' seconds."
  660.   (interactive "SDelay Flyspell after Command: ")
  661.   (put command 'flyspell-delayed t))
  662.  
  663. ;;*---------------------------------------------------------------------*/
  664. ;;*    flyspell-deplacement-commands ...                                */
  665. ;;*---------------------------------------------------------------------*/
  666. (defun flyspell-deplacement-commands ()
  667.   "Install the standard set of Flyspell deplacement commands."
  668.   (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands)
  669.   (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
  670.  
  671. ;;*---------------------------------------------------------------------*/
  672. ;;*    flyspell-deplacement-command ...                                 */
  673. ;;*---------------------------------------------------------------------*/
  674. (defun flyspell-deplacement-command (command)
  675.   "Set COMMAND that implement cursor movements, for Flyspell.
  676. When flyspell `post-command-hook' is invoked because of a deplacement command
  677. as been used the current word is checked only if the previous command was
  678. not the very same deplacement command."
  679.   (interactive "SDeplacement Flyspell after Command: ")
  680.   (put command 'flyspell-deplacement t))
  681.  
  682. ;;*---------------------------------------------------------------------*/
  683. ;;*    flyspell-word-cache ...                                          */
  684. ;;*---------------------------------------------------------------------*/
  685. (defvar flyspell-word-cache-start  nil)
  686. (defvar flyspell-word-cache-end    nil)
  687. (defvar flyspell-word-cache-word   nil)
  688. (defvar flyspell-word-cache-result '_)
  689. (make-variable-buffer-local 'flyspell-word-cache-start)
  690. (make-variable-buffer-local 'flyspell-word-cache-end)
  691. (make-variable-buffer-local 'flyspell-word-cache-word)
  692. (make-variable-buffer-local 'flyspell-word-cache-result)
  693.  
  694. ;;*---------------------------------------------------------------------*/
  695. ;;*    The flyspell pre-hook, store the current position. In the        */
  696. ;;*    post command hook, we will check, if the word at this position   */
  697. ;;*    has to be spell checked.                                         */
  698. ;;*---------------------------------------------------------------------*/
  699. (defvar flyspell-pre-buffer     nil)
  700. (defvar flyspell-pre-point      nil)
  701. (defvar flyspell-pre-column     nil)
  702. (defvar flyspell-pre-pre-buffer nil)
  703. (defvar flyspell-pre-pre-point  nil)
  704. (make-variable-buffer-local 'flyspell-pre-point)
  705.  
  706. ;;*---------------------------------------------------------------------*/
  707. ;;*    flyspell-previous-command ...                                    */
  708. ;;*---------------------------------------------------------------------*/
  709. (defvar flyspell-previous-command nil
  710.   "The last interactive command checked by Flyspell.")
  711.  
  712. ;;*---------------------------------------------------------------------*/
  713. ;;*    flyspell-pre-command-hook ...                                    */
  714. ;;*---------------------------------------------------------------------*/
  715. (defun flyspell-pre-command-hook ()
  716.   "Save the current buffer and point for Flyspell's post-command hook."
  717.   (interactive)
  718.   (setq flyspell-pre-buffer (current-buffer))
  719.   (setq flyspell-pre-point  (point))
  720.   (setq flyspell-pre-column (current-column)))
  721.  
  722. ;;*---------------------------------------------------------------------*/
  723. ;;*    flyspell-mode-off ...                                            */
  724. ;;*---------------------------------------------------------------------*/
  725. ;;;###autoload
  726. (defun flyspell-mode-off ()
  727.   "Turn Flyspell mode off."
  728.   ;; we remove the hooks
  729.   (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
  730.   (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
  731.   (remove-hook 'after-change-functions 'flyspell-after-change-function t)
  732.   (remove-hook 'hack-local-variables-hook
  733.            (function flyspell-hack-local-variables-hook) t)
  734.   ;; we remove all the flyspell hilightings
  735.   (flyspell-delete-all-overlays)
  736.   ;; we have to erase pre cache variables
  737.   (setq flyspell-pre-buffer nil)
  738.   (setq flyspell-pre-point  nil)
  739.   ;; we mark the mode as killed
  740.   (setq flyspell-mode nil))
  741.  
  742. ;;*---------------------------------------------------------------------*/
  743. ;;*    flyspell-check-pre-word-p ...                                    */
  744. ;;*---------------------------------------------------------------------*/
  745. (defun flyspell-check-pre-word-p ()
  746.   "Return non-nil if we should check the word before point.
  747. More precisely, it applies to the word that was before point
  748. before the current command."
  749.   (cond
  750.    ((or (not (numberp flyspell-pre-point))
  751.     (not (bufferp flyspell-pre-buffer))
  752.     (not (buffer-live-p flyspell-pre-buffer)))
  753.     nil)
  754.    ((and (eq flyspell-pre-pre-point flyspell-pre-point)
  755.      (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
  756.     nil)
  757.    ((or (and (= flyspell-pre-point (- (point) 1))
  758.          (eq (char-syntax (char-after flyspell-pre-point)) ?w))
  759.     (= flyspell-pre-point (point))
  760.     (= flyspell-pre-point (+ (point) 1)))
  761.     nil)
  762.    ((and (symbolp this-command)
  763.      (not executing-kbd-macro)
  764.      (or (get this-command 'flyspell-delayed)
  765.          (and (get this-command 'flyspell-deplacement)
  766.           (eq flyspell-previous-command this-command)))
  767.      (or (= (current-column) 0)
  768.          (= (current-column) flyspell-pre-column)
  769.          ;; If other post-command-hooks change the buffer,
  770.          ;; flyspell-pre-point can lie past eob (bug#468).
  771.          (null (char-after flyspell-pre-point))
  772.          (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
  773.     nil)
  774.    ((not (eq (current-buffer) flyspell-pre-buffer))
  775.     t)
  776.    ((not (and (numberp flyspell-word-cache-start)
  777.           (numberp flyspell-word-cache-end)))
  778.     t)
  779.    (t
  780.     (or (< flyspell-pre-point flyspell-word-cache-start)
  781.     (> flyspell-pre-point flyspell-word-cache-end)))))
  782.  
  783. ;;*---------------------------------------------------------------------*/
  784. ;;*    The flyspell after-change-hook, store the change position. In    */
  785. ;;*    the post command hook, we will check, if the word at this        */
  786. ;;*    position has to be spell checked.                                */
  787. ;;*---------------------------------------------------------------------*/
  788. (defvar flyspell-changes nil)
  789. (make-variable-buffer-local 'flyspell-changes)
  790.  
  791. ;;*---------------------------------------------------------------------*/
  792. ;;*    flyspell-after-change-function ...                               */
  793. ;;*---------------------------------------------------------------------*/
  794. (defun flyspell-after-change-function (start stop len)
  795.   "Save the current buffer and point for Flyspell's post-command hook."
  796.   (push (cons start stop) flyspell-changes))
  797.  
  798. ;;*---------------------------------------------------------------------*/
  799. ;;*    flyspell-check-changed-word-p ...                                */
  800. ;;*---------------------------------------------------------------------*/
  801. (defun flyspell-check-changed-word-p (start stop)
  802.   "Return t when the changed word has to be checked.
  803. The answer depends of several criteria.
  804. Mostly we check word delimiters."
  805.   (cond
  806.    ((and (memq (char-after start) '(?\n ? )) (> stop start))
  807.     t)
  808.    ((not (numberp flyspell-pre-point))
  809.     t)
  810.    ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
  811.     nil)
  812.    ((let ((pos (point)))
  813.       (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
  814.     nil)
  815.    (t
  816.     t)))
  817.  
  818. ;;*---------------------------------------------------------------------*/
  819. ;;*    flyspell-check-word-p ...                                        */
  820. ;;*---------------------------------------------------------------------*/
  821. (defun flyspell-check-word-p ()
  822.   "Return t when the word at `point' has to be checked.
  823. The answer depends of several criteria.
  824. Mostly we check word delimiters."
  825.   (cond
  826.    ((<= (- (point-max) 1) (point-min))
  827.     ;; the buffer is not filled enough
  828.     nil)
  829.    ((and (and (> (current-column) 0)
  830.           (not (eq (current-column) flyspell-pre-column)))
  831.      (save-excursion
  832.        (backward-char 1)
  833.        (and (looking-at (flyspell-get-not-casechars))
  834.         (or flyspell-consider-dash-as-word-delimiter-flag
  835.             (not (looking-at "-"))))))
  836.     ;; yes because we have reached or typed a word delimiter.
  837.     t)
  838.    ((symbolp this-command)
  839.     (cond
  840.      ((get this-command 'flyspell-deplacement)
  841.       (not (eq flyspell-previous-command this-command)))
  842.      ((get this-command 'flyspell-delayed)
  843.       ;; the current command is not delayed, that
  844.       ;; is that we must check the word now
  845.       (and (not unread-command-events)
  846.        (sit-for flyspell-delay)))
  847.      (t t)))
  848.    (t t)))
  849.  
  850. ;;*---------------------------------------------------------------------*/
  851. ;;*    flyspell-debug-signal-no-check ...                               */
  852. ;;*---------------------------------------------------------------------*/
  853. (defun flyspell-debug-signal-no-check (msg obj)
  854.   (setq debug-on-error t)
  855.   (with-current-buffer (get-buffer-create "*flyspell-debug*")
  856.     (erase-buffer)
  857.     (insert "NO-CHECK:\n")
  858.     (insert (format "    %S : %S\n" msg obj))))
  859.  
  860. ;;*---------------------------------------------------------------------*/
  861. ;;*    flyspell-debug-signal-pre-word-checked ...                       */
  862. ;;*---------------------------------------------------------------------*/
  863. (defun flyspell-debug-signal-pre-word-checked ()
  864.   (setq debug-on-error t)
  865.   (with-current-buffer (get-buffer-create "*flyspell-debug*")
  866.     (insert "PRE-WORD:\n")
  867.     (insert (format "  pre-point  : %S\n" flyspell-pre-point))
  868.     (insert (format "  pre-buffer : %S\n" flyspell-pre-buffer))
  869.     (insert (format "  cache-start: %S\n" flyspell-word-cache-start))
  870.     (insert (format "  cache-end  : %S\n" flyspell-word-cache-end))
  871.     (goto-char (point-max))))
  872.  
  873. ;;*---------------------------------------------------------------------*/
  874. ;;*    flyspell-debug-signal-word-checked ...                           */
  875. ;;*---------------------------------------------------------------------*/
  876. (defun flyspell-debug-signal-word-checked ()
  877.   (setq debug-on-error t)
  878.   (let ((oldbuf (current-buffer))
  879.         (point  (point)))
  880.     (with-current-buffer (get-buffer-create "*flyspell-debug*")
  881.       (insert "WORD:\n")
  882.       (insert (format "  this-cmd   : %S\n" this-command))
  883.       (insert (format "  delayed    : %S\n" (and (symbolp this-command)
  884.                          (get this-command 'flyspell-delayed))))
  885.       (insert (format "  point      : %S\n" point))
  886.       (insert (format "  prev-char  : [%c] %S\n"
  887.               (with-current-buffer oldbuf
  888.             (let ((c (if (> (point) (point-min))
  889.                      (save-excursion
  890.                        (backward-char 1)
  891.                        (char-after (point)))
  892.                    ? )))
  893.               c))
  894.               (with-current-buffer oldbuf
  895.             (let ((c (if (> (point) (point-min))
  896.                      (save-excursion
  897.                        (backward-char 1)
  898.                        (and (and (looking-at (flyspell-get-not-casechars)) 1)
  899.                         (and (or flyspell-consider-dash-as-word-delimiter-flag
  900.                              (not (looking-at "\\-"))) 2))))))
  901.               c))))
  902.       (insert (format "  because    : %S\n"
  903.               (cond
  904.                ((not (and (symbolp this-command)
  905.                   (get this-command 'flyspell-delayed)))
  906.             ;; the current command is not delayed, that
  907.             ;; is that we must check the word now
  908.             'not-delayed)
  909.                ((with-current-buffer oldbuf
  910.               (let ((c (if (> (point) (point-min))
  911.                        (save-excursion
  912.                      (backward-char 1)
  913.                      (and (looking-at (flyspell-get-not-casechars))
  914.                           (or flyspell-consider-dash-as-word-delimiter-flag
  915.                           (not (looking-at "\\-"))))))))
  916.                 c))
  917.             ;; yes because we have reached or typed a word delimiter.
  918.             'separator)
  919.                ((not (integerp flyspell-delay))
  920.             ;; yes because the user had set up a no-delay configuration.
  921.             'no-delay)
  922.                (t
  923.             'sit-for))))
  924.       (goto-char (point-max)))))
  925.  
  926. ;;*---------------------------------------------------------------------*/
  927. ;;*    flyspell-debug-signal-changed-checked ...                        */
  928. ;;*---------------------------------------------------------------------*/
  929. (defun flyspell-debug-signal-changed-checked ()
  930.   (setq debug-on-error t)
  931.   (let ((point  (point)))
  932.     (with-current-buffer (get-buffer-create "*flyspell-debug*")
  933.       (insert "CHANGED WORD:\n")
  934.       (insert (format "  point   : %S\n" point))
  935.       (goto-char (point-max)))))
  936.  
  937. ;;*---------------------------------------------------------------------*/
  938. ;;*    flyspell-post-command-hook ...                                   */
  939. ;;*    -------------------------------------------------------------    */
  940. ;;*    It is possible that we check several words:                      */
  941. ;;*    1- the current word is checked if the predicate                  */
  942. ;;*       FLYSPELL-CHECK-WORD-P is true                                 */
  943. ;;*    2- the word that used to be the current word before the          */
  944. ;;*       THIS-COMMAND is checked if:                                   */
  945. ;;*        a- the previous word is different from the current word      */
  946. ;;*        b- the previous word as not just been checked by the         */
  947. ;;*           previous FLYSPELL-POST-COMMAND-HOOK                       */
  948. ;;*    3- the words changed by the THIS-COMMAND that are neither the    */
  949. ;;*       previous word nor the current word                            */
  950. ;;*---------------------------------------------------------------------*/
  951. (defun flyspell-post-command-hook ()
  952.   "The `post-command-hook' used by flyspell to check a word in-the-fly."
  953.   (interactive)
  954.   (when flyspell-mode
  955.     (let ((command this-command)
  956.       ;; Prevent anything we do from affecting the mark.
  957.       deactivate-mark)
  958.       (if (flyspell-check-pre-word-p)
  959.       (with-current-buffer flyspell-pre-buffer
  960.         '(flyspell-debug-signal-pre-word-checked)
  961.         (save-excursion
  962.           (goto-char flyspell-pre-point)
  963.           (flyspell-word))))
  964.       (if (flyspell-check-word-p)
  965.       (progn
  966.         '(flyspell-debug-signal-word-checked)
  967.         (flyspell-word)
  968.         ;; we remember which word we have just checked.
  969.         ;; this will be used next time we will check a word
  970.         ;; to compare the next current word with the word
  971.         ;; that as been registered in the pre-command-hook
  972.         ;; that is these variables are used within the predicate
  973.         ;; FLYSPELL-CHECK-PRE-WORD-P
  974.         (setq flyspell-pre-pre-buffer (current-buffer))
  975.         (setq flyspell-pre-pre-point  (point)))
  976.     (progn
  977.       (setq flyspell-pre-pre-buffer nil)
  978.       (setq flyspell-pre-pre-point  nil)
  979.       ;; when a word is not checked because of a delayed command
  980.       ;; we do not disable the ispell cache.
  981.       (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
  982.           (progn
  983.         (setq flyspell-word-cache-end -1)
  984.         (setq flyspell-word-cache-result '_)))))
  985.       (while (and (not (input-pending-p)) (consp flyspell-changes))
  986.     (let ((start (car (car flyspell-changes)))
  987.           (stop  (cdr (car flyspell-changes))))
  988.       (if (flyspell-check-changed-word-p start stop)
  989.           (save-excursion
  990.         '(flyspell-debug-signal-changed-checked)
  991.         (goto-char start)
  992.         (flyspell-word)))
  993.       (setq flyspell-changes (cdr flyspell-changes))))
  994.       (setq flyspell-previous-command command))))
  995.  
  996. ;;*---------------------------------------------------------------------*/
  997. ;;*    flyspell-notify-misspell ...                                     */
  998. ;;*---------------------------------------------------------------------*/
  999. (defun flyspell-notify-misspell (word poss)
  1000.   (let ((replacements (if (stringp poss)
  1001.               poss
  1002.             (if flyspell-sort-corrections
  1003.                 (sort (car (cdr (cdr poss))) 'string<)
  1004.               (car (cdr (cdr poss)))))))
  1005.     (if flyspell-issue-message-flag
  1006.     (message "misspelling `%s'  %S" word replacements))))
  1007.  
  1008. ;;*---------------------------------------------------------------------*/
  1009. ;;*    flyspell-word-search-backward ...                                */
  1010. ;;*---------------------------------------------------------------------*/
  1011. (defun flyspell-word-search-backward (word bound)
  1012.   (save-excursion
  1013.     (let ((r '())
  1014.       (inhibit-point-motion-hooks t)
  1015.       p)
  1016.       (while (and (not r) (setq p (search-backward word bound t)))
  1017.     (let ((lw (flyspell-get-word '())))
  1018.       (if (and (consp lw) (string-equal (car lw) word))
  1019.           (setq r p)
  1020.         (goto-char p))))
  1021.       r)))
  1022.  
  1023. ;;*---------------------------------------------------------------------*/
  1024. ;;*    flyspell-word-search-forward ...                                 */
  1025. ;;*---------------------------------------------------------------------*/
  1026. (defun flyspell-word-search-forward (word bound)
  1027.   (save-excursion
  1028.     (let ((r '())
  1029.       (inhibit-point-motion-hooks t)
  1030.       p)
  1031.       (while (and (not r) (setq p (search-forward word bound t)))
  1032.     (let ((lw (flyspell-get-word '())))
  1033.       (if (and (consp lw) (string-equal (car lw) word))
  1034.           (setq r p)
  1035.         (goto-char (1+ p)))))
  1036.       r)))
  1037.  
  1038. ;;*---------------------------------------------------------------------*/
  1039. ;;*    flyspell-word ...                                                */
  1040. ;;*---------------------------------------------------------------------*/
  1041. (defun flyspell-word (&optional following)
  1042.   "Spell check a word."
  1043.   (interactive (list ispell-following-word))
  1044.   (ispell-set-spellchecker-params)    ; Initialize variables and dicts alists
  1045.   (save-excursion
  1046.     ;; use the correct dictionary
  1047.     (flyspell-accept-buffer-local-defs)
  1048.     (let* ((cursor-location (point))
  1049.            (flyspell-word (flyspell-get-word following))
  1050.            start end poss word ispell-filter)
  1051.       (if (or (eq flyspell-word nil)
  1052.            (and (fboundp flyspell-generic-check-word-predicate)
  1053.             (not (funcall flyspell-generic-check-word-predicate))))
  1054.       t
  1055.     (progn
  1056.       ;; destructure return flyspell-word info list.
  1057.       (setq start (car (cdr flyspell-word))
  1058.         end (car (cdr (cdr flyspell-word)))
  1059.         word (car flyspell-word))
  1060.       ;; before checking in the directory, we check for doublons.
  1061.       (cond
  1062.        ((and (or (not (eq ispell-parser 'tex))
  1063.              (and (> start (point-min))
  1064.               (not (memq (char-after (1- start)) '(?\} ?\\)))))
  1065.          flyspell-mark-duplications-flag
  1066.          (not (catch 'exception
  1067.             (dolist (except flyspell-mark-duplications-exceptions)
  1068.               (and (string= (or ispell-local-dictionary
  1069.                         ispell-dictionary)
  1070.                     (car except))
  1071.                    (member (downcase word) (cdr except))
  1072.                    (throw 'exception t)))))
  1073.          (save-excursion
  1074.            (goto-char start)
  1075.            (let* ((bound
  1076.                (- start
  1077.                   (- end start)
  1078.                   (- (skip-chars-backward " \t\n\f"))))
  1079.               (p (when (>= bound (point-min))
  1080.                    (flyspell-word-search-backward word bound))))
  1081.              (and p (/= p start)))))
  1082.         ;; yes, this is a doublon
  1083.         (flyspell-highlight-incorrect-region start end 'doublon)
  1084.         nil)
  1085.        ((and (eq flyspell-word-cache-start start)
  1086.          (eq flyspell-word-cache-end end)
  1087.          (string-equal flyspell-word-cache-word word))
  1088.         ;; this word had been already checked, we skip
  1089.         flyspell-word-cache-result)
  1090.        ((and (eq ispell-parser 'tex)
  1091.          (flyspell-tex-command-p flyspell-word))
  1092.         ;; this is a correct word (because a tex command)
  1093.         (flyspell-unhighlight-at start)
  1094.         (if (> end start)
  1095.         (flyspell-unhighlight-at (- end 1)))
  1096.         t)
  1097.        (t
  1098.         ;; we setup the cache
  1099.         (setq flyspell-word-cache-start start)
  1100.         (setq flyspell-word-cache-end end)
  1101.         (setq flyspell-word-cache-word word)
  1102.         ;; now check spelling of word.
  1103.         (ispell-send-string "%\n")
  1104.         ;; put in verbose mode
  1105.         (ispell-send-string (concat "^" word "\n"))
  1106.         ;; we mark the ispell process so it can be killed
  1107.         ;; when emacs is exited without query
  1108.         (if (fboundp 'set-process-query-on-exit-flag)
  1109.         (set-process-query-on-exit-flag ispell-process nil)
  1110.           (if (fboundp 'process-kill-without-query)
  1111.           (process-kill-without-query ispell-process)))
  1112.         ;; Wait until ispell has processed word.  Since this code is often
  1113.             ;; executed from post-command-hook but the ispell process may not
  1114.             ;; be responsive, it's important to make sure we re-enable C-g.
  1115.         (if (fboundp 'with-local-quit)
  1116.         (with-local-quit
  1117.           (while (progn
  1118.                (accept-process-output ispell-process)
  1119.                (not (string= "" (car ispell-filter))))))
  1120.           (while (progn
  1121.                (accept-process-output ispell-process)
  1122.                (not (string= "" (car ispell-filter))))))
  1123.         ;; (ispell-send-string "!\n")
  1124.         ;; back to terse mode.
  1125.         ;; Remove leading empty element
  1126.         (setq ispell-filter (cdr ispell-filter))
  1127.         ;; ispell process should return something after word is sent.
  1128.         ;; Tag word as valid (i.e., skip) otherwise
  1129.         (or ispell-filter
  1130.         (setq ispell-filter '(*)))
  1131.         (if (consp ispell-filter)
  1132.         (setq poss (ispell-parse-output (car ispell-filter))))
  1133.         (let ((res (cond ((eq poss t)
  1134.                   ;; correct
  1135.                   (setq flyspell-word-cache-result t)
  1136.                   (flyspell-unhighlight-at start)
  1137.                   (if (> end start)
  1138.                   (flyspell-unhighlight-at (- end 1)))
  1139.                   t)
  1140.                  ((and (stringp poss) flyspell-highlight-flag)
  1141.                   ;; correct
  1142.                   (setq flyspell-word-cache-result t)
  1143.                   (flyspell-unhighlight-at start)
  1144.                   (if (> end start)
  1145.                   (flyspell-unhighlight-at (- end 1)))
  1146.                   t)
  1147.                  ((null poss)
  1148.                   (setq flyspell-word-cache-result t)
  1149.                   (flyspell-unhighlight-at start)
  1150.                   (if (> end start)
  1151.                   (flyspell-unhighlight-at (- end 1)))
  1152.                   t)
  1153.                  ((or (and (< flyspell-duplicate-distance 0)
  1154.                        (or (save-excursion
  1155.                          (goto-char start)
  1156.                          (flyspell-word-search-backward
  1157.                           word
  1158.                           (point-min)))
  1159.                        (save-excursion
  1160.                          (goto-char end)
  1161.                          (flyspell-word-search-forward
  1162.                           word
  1163.                           (point-max)))))
  1164.                   (and (> flyspell-duplicate-distance 0)
  1165.                        (or (save-excursion
  1166.                          (goto-char start)
  1167.                          (flyspell-word-search-backward
  1168.                           word
  1169.                           (- start
  1170.                          flyspell-duplicate-distance)))
  1171.                        (save-excursion
  1172.                          (goto-char end)
  1173.                          (flyspell-word-search-forward
  1174.                           word
  1175.                           (+ end
  1176.                          flyspell-duplicate-distance))))))
  1177.                   ;; This is a misspelled word which occurs
  1178.                   ;; twice within flyspell-duplicate-distance.
  1179.                   (setq flyspell-word-cache-result nil)
  1180.                   (if flyspell-highlight-flag
  1181.                   (flyspell-highlight-duplicate-region
  1182.                    start end poss)
  1183.                 (message "duplicate `%s'" word))
  1184.                   nil)
  1185.                  (t
  1186.                   (setq flyspell-word-cache-result nil)
  1187.                   ;; incorrect highlight the location
  1188.                   (if flyspell-highlight-flag
  1189.                   (flyspell-highlight-incorrect-region
  1190.                    start end poss)
  1191.                 (flyspell-notify-misspell word poss))
  1192.                   nil))))
  1193.           ;; return to original location
  1194.           (goto-char cursor-location)
  1195.           (if ispell-quit (setq ispell-quit nil))
  1196.           res))))))))
  1197.  
  1198. ;;*---------------------------------------------------------------------*/
  1199. ;;*    flyspell-math-tex-command-p ...                                  */
  1200. ;;*    -------------------------------------------------------------    */
  1201. ;;*    This function uses the texmathp package to check if point        */
  1202. ;;*    is within a TeX math environment. `texmathp' can yield errors    */
  1203. ;;*    if the document is currently not valid TeX syntax.               */
  1204. ;;*---------------------------------------------------------------------*/
  1205. (defun flyspell-math-tex-command-p ()
  1206.   (when (fboundp 'texmathp)
  1207.     (if flyspell-check-tex-math-command
  1208.         nil
  1209.       (condition-case nil
  1210.           (texmathp)
  1211.         (error nil)))))
  1212.  
  1213. ;;*---------------------------------------------------------------------*/
  1214. ;;*    flyspell-tex-command-p ...                                       */
  1215. ;;*---------------------------------------------------------------------*/
  1216. (defun flyspell-tex-command-p (word)
  1217.   "Return t if WORD is a TeX command."
  1218.   (or (save-excursion
  1219.     (let ((b  (car (cdr word))))
  1220.       (and (re-search-backward "\\\\" (- (point) 100) t)
  1221.            (or (= (match-end 0) b)
  1222.            (and (goto-char (match-end 0))
  1223.             (looking-at flyspell-tex-command-regexp)
  1224.             (>= (match-end 0) b))))))
  1225.       (flyspell-math-tex-command-p)))
  1226.  
  1227. ;;*---------------------------------------------------------------------*/
  1228. ;;*    flyspell-casechars-cache ...                                     */
  1229. ;;*---------------------------------------------------------------------*/
  1230. (defvar flyspell-casechars-cache nil)
  1231. (defvar flyspell-ispell-casechars-cache nil)
  1232. (make-variable-buffer-local 'flyspell-casechars-cache)
  1233. (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
  1234.  
  1235. ;;*---------------------------------------------------------------------*/
  1236. ;;*    flyspell-get-casechars ...                                       */
  1237. ;;*---------------------------------------------------------------------*/
  1238. (defun flyspell-get-casechars ()
  1239.   "This function builds a string that is the regexp of word chars.
  1240. In order to avoid one useless string construction,
  1241. this function changes the last char of the `ispell-casechars' string."
  1242.   (let ((ispell-casechars (ispell-get-casechars)))
  1243.     (cond
  1244.      ((eq ispell-parser 'tex)
  1245.       (setq flyspell-ispell-casechars-cache ispell-casechars)
  1246.       (setq flyspell-casechars-cache
  1247.         (concat (substring ispell-casechars
  1248.                    0
  1249.                    (- (length ispell-casechars) 1))
  1250.             "]"))
  1251.       flyspell-casechars-cache)
  1252.      (t
  1253.       (setq flyspell-ispell-casechars-cache ispell-casechars)
  1254.       (setq flyspell-casechars-cache ispell-casechars)
  1255.       flyspell-casechars-cache))))
  1256.  
  1257. ;;*---------------------------------------------------------------------*/
  1258. ;;*    flyspell-get-not-casechars-cache ...                             */
  1259. ;;*---------------------------------------------------------------------*/
  1260. (defvar flyspell-not-casechars-cache nil)
  1261. (defvar flyspell-ispell-not-casechars-cache nil)
  1262. (make-variable-buffer-local 'flyspell-not-casechars-cache)
  1263. (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
  1264.  
  1265. ;;*---------------------------------------------------------------------*/
  1266. ;;*    flyspell-get-not-casechars ...                                   */
  1267. ;;*---------------------------------------------------------------------*/
  1268. (defun flyspell-get-not-casechars ()
  1269.   "This function builds a string that is the regexp of non-word chars."
  1270.   (let ((ispell-not-casechars (ispell-get-not-casechars)))
  1271.     (cond
  1272.      ((eq ispell-parser 'tex)
  1273.       (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
  1274.       (setq flyspell-not-casechars-cache
  1275.         (concat (substring ispell-not-casechars
  1276.                    0
  1277.                    (- (length ispell-not-casechars) 1))
  1278.             "]"))
  1279.       flyspell-not-casechars-cache)
  1280.      (t
  1281.       (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
  1282.       (setq flyspell-not-casechars-cache ispell-not-casechars)
  1283.       flyspell-not-casechars-cache))))
  1284.  
  1285. ;;*---------------------------------------------------------------------*/
  1286. ;;*    flyspell-get-word ...                                            */
  1287. ;;*---------------------------------------------------------------------*/
  1288. (defun flyspell-get-word (following &optional extra-otherchars)
  1289.   "Return the word for spell-checking according to Ispell syntax.
  1290. If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
  1291. is non-nil when called interactively, then the following word
  1292. \(rather than preceding\) is checked when the cursor is not over a word.
  1293. Optional second argument contains otherchars that can be included in word
  1294. many times.
  1295.  
  1296. Word syntax described by `flyspell-dictionary-alist' (which see)."
  1297.   (let* ((flyspell-casechars (flyspell-get-casechars))
  1298.      (flyspell-not-casechars (flyspell-get-not-casechars))
  1299.      (ispell-otherchars (ispell-get-otherchars))
  1300.      (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
  1301.      (word-regexp (concat flyspell-casechars
  1302.                   "+\\("
  1303.                   (if (not (string= "" ispell-otherchars))
  1304.                   (concat ispell-otherchars "?"))
  1305.                   (if extra-otherchars
  1306.                   (concat extra-otherchars "?"))
  1307.                   flyspell-casechars
  1308.                   "+\\)"
  1309.                   (if (or ispell-many-otherchars-p
  1310.                       extra-otherchars)
  1311.                   "*" "?")))
  1312.      did-it-once prevpt
  1313.      start end word)
  1314.     ;; find the word
  1315.     (if (not (looking-at flyspell-casechars))
  1316.     (if following
  1317.         (re-search-forward flyspell-casechars nil t)
  1318.       (re-search-backward flyspell-casechars nil t)))
  1319.     ;; move to front of word
  1320.     (re-search-backward flyspell-not-casechars nil 'start)
  1321.     (while (and (or (and (not (string= "" ispell-otherchars))
  1322.              (looking-at ispell-otherchars))
  1323.             (and extra-otherchars (looking-at extra-otherchars)))
  1324.         (not (bobp))
  1325.         (or (not did-it-once)
  1326.             ispell-many-otherchars-p)
  1327.         (not (eq prevpt (point))))
  1328.       (if (and extra-otherchars (looking-at extra-otherchars))
  1329.       (progn
  1330.         (backward-char 1)
  1331.         (if (looking-at flyspell-casechars)
  1332.         (re-search-backward flyspell-not-casechars nil 'move)))
  1333.     (setq did-it-once t
  1334.           prevpt (point))
  1335.     (backward-char 1)
  1336.     (if (looking-at flyspell-casechars)
  1337.         (re-search-backward flyspell-not-casechars nil 'move)
  1338.       (backward-char -1))))
  1339.     ;; Now mark the word and save to string.
  1340.     (if (not (re-search-forward word-regexp nil t))
  1341.     nil
  1342.       (progn
  1343.     (setq start (match-beginning 0)
  1344.           end (point)
  1345.           word (buffer-substring-no-properties start end))
  1346.     (list word start end)))))
  1347.  
  1348. ;;*---------------------------------------------------------------------*/
  1349. ;;*    flyspell-small-region ...                                        */
  1350. ;;*---------------------------------------------------------------------*/
  1351. (defun flyspell-small-region (beg end)
  1352.   "Flyspell text between BEG and END."
  1353.   (save-excursion
  1354.     (if (> beg end)
  1355.     (let ((old beg))
  1356.       (setq beg end)
  1357.       (setq end old)))
  1358.     (goto-char beg)
  1359.     (let ((count 0))
  1360.       (while (< (point) end)
  1361.     (if (and flyspell-issue-message-flag (= count 100))
  1362.         (progn
  1363.           (message "Spell Checking...%d%%"
  1364.                (* 100 (/ (float (- (point) beg)) (- end beg))))
  1365.           (setq count 0))
  1366.       (setq count (+ 1 count)))
  1367.     (flyspell-word)
  1368.     (sit-for 0)
  1369.     (let ((cur (point)))
  1370.       (forward-word 1)
  1371.       (if (and (< (point) end) (> (point) (+ cur 1)))
  1372.           (backward-char 1)))))
  1373.     (backward-char 1)
  1374.     (if flyspell-issue-message-flag (message "Spell Checking completed."))
  1375.     (flyspell-word)))
  1376.  
  1377. ;;*---------------------------------------------------------------------*/
  1378. ;;*    flyspell-external-ispell-process ...                             */
  1379. ;;*---------------------------------------------------------------------*/
  1380. (defvar flyspell-external-ispell-process '()
  1381.   "The external Flyspell Ispell process.")
  1382.  
  1383. ;;*---------------------------------------------------------------------*/
  1384. ;;*    flyspell-external-ispell-buffer ...                              */
  1385. ;;*---------------------------------------------------------------------*/
  1386. (defvar flyspell-external-ispell-buffer '())
  1387. (defvar flyspell-large-region-buffer '())
  1388. (defvar flyspell-large-region-beg (point-min))
  1389. (defvar flyspell-large-region-end (point-max))
  1390.  
  1391. ;;*---------------------------------------------------------------------*/
  1392. ;;*    flyspell-external-point-words ...                                */
  1393. ;;*---------------------------------------------------------------------*/
  1394. (defun flyspell-external-point-words ()
  1395.   "Mark words from a buffer listing incorrect words in order of appearance.
  1396. The list of incorrect words should be in `flyspell-external-ispell-buffer'.
  1397. \(We finish by killing that buffer and setting the variable to nil.)
  1398. The buffer to mark them in is `flyspell-large-region-buffer'."
  1399.   (let (words-not-found
  1400.     (ispell-otherchars (ispell-get-otherchars))
  1401.     (buffer-scan-pos flyspell-large-region-beg)
  1402.     case-fold-search
  1403.     misspell-mismatches
  1404.     (debian-debug (and (boundp 'debian-dict-common-debug)
  1405.                debian-dict-common-debug)))
  1406.     (with-current-buffer flyspell-external-ispell-buffer
  1407.       (goto-char (point-min))
  1408.       ;; Loop over incorrect words, in the order they were reported,
  1409.       ;; which is also the order they appear in the buffer being checked.
  1410.       (while (re-search-forward "\\([^\n]+\\)\n" nil t)
  1411.     ;; Bind WORD to the next one.
  1412.     (let ((word (match-string 1)) (wordpos (point)))
  1413.       ;; Here there used to be code to see if WORD is the same
  1414.       ;; as the previous iteration, and count the number of consecutive
  1415.       ;; identical words, and the loop below would search for that many.
  1416.       ;; That code seemed to be incorrect, and on principle, should
  1417.       ;; be unnecessary too. -- rms.
  1418.       (if flyspell-issue-message-flag
  1419.           (message "Spell Checking...%d%% [%s]"
  1420.                (* 100 (/ (float (point)) (point-max)))
  1421.                word))
  1422.       (with-current-buffer flyspell-large-region-buffer
  1423.         (goto-char buffer-scan-pos)
  1424.         (let ((keep t))
  1425.           ;; Iterate on string search until string is found as word,
  1426.           ;; not as substring
  1427.           (while keep
  1428.         (if (search-forward word
  1429.                     flyspell-large-region-end t)
  1430.             (let* ((found-list
  1431.                 (save-excursion
  1432.                   ;; Move back into the match
  1433.                   ;; so flyspell-get-word will find it.
  1434.                   (forward-char -1)
  1435.                   (flyspell-get-word nil)))
  1436.                (found (car found-list))
  1437.                (found-length (length found))
  1438.                (misspell-length (length word)))
  1439.               (when (or
  1440.                  ;; Size matches, we really found it.
  1441.                  (= found-length misspell-length)
  1442.                  ;; Matches as part of a boundary-char separated word
  1443.                  (member word
  1444.                      (split-string found ispell-otherchars))
  1445.                  ;; Misspelling has higher length than
  1446.                  ;; what flyspell considers the
  1447.                  ;; word.  Caused by boundary-chars
  1448.                  ;; mismatch.  Validating seems safe.
  1449.                  (and (< found-length misspell-length)
  1450.                   (add-to-list 'misspell-mismatches
  1451.                            (concat "ispell: " word
  1452.                                ", flyspell-get-word: " found)))
  1453.                  ;; ispell treats beginning of some TeX
  1454.                  ;; commands as nroff control sequences
  1455.                  ;; and strips them in the list of
  1456.                  ;; misspelled words thus giving a
  1457.                  ;; non-existent word.  Skip if ispell
  1458.                  ;; is used, string is a TeX command
  1459.                  ;; (char before beginning of word is
  1460.                  ;; backslash) and none of the previous
  1461.                  ;; contitions match
  1462.                  (and (not ispell-really-aspell)
  1463.                   (save-excursion
  1464.                     (goto-char (- (nth 1 found-list) 1))
  1465.                     (if (looking-at "[\\]" )
  1466.                     t
  1467.                       nil))))
  1468.             (setq keep nil)
  1469.             (flyspell-word)
  1470.             ;; Search for next misspelled word will begin from
  1471.             ;; end of last validated match.
  1472.             (setq buffer-scan-pos (point))))
  1473.           ;; Record if misspelling is not found and try new one
  1474.           (add-to-list 'words-not-found
  1475.                    (concat " -> " word " - "
  1476.                        (int-to-string wordpos)))
  1477.           (setq keep nil)))))))
  1478.       ;; we are done
  1479.       (if flyspell-issue-message-flag (message "Spell Checking completed.")))
  1480.     ;; Warn about not found misspellings
  1481.     (dolist (word words-not-found)
  1482.       (message "%s: word not found" word))
  1483.     (when (and debian-debug misspell-mismatches)
  1484.       (message "Misspelling length is higher than what flyspell-get-word considers
  1485. a word. Possible boundary chars or encoding mismatch:")
  1486.       (dolist (mismatch misspell-mismatches)
  1487.     (message " %s:     misspelled and found string mismatch" mismatch)))
  1488.     ;; Kill and forget the buffer with the list of incorrect words.
  1489.     (unless debian-debug
  1490.       (kill-buffer flyspell-external-ispell-buffer)
  1491.       (setq flyspell-external-ispell-buffer nil))))
  1492.  
  1493. ;;*---------------------------------------------------------------------*/
  1494. ;;*    flyspell-process-localwords ...                                  */
  1495. ;;*    -------------------------------------------------------------    */
  1496. ;;*    This function is used to prevent marking of words explicitly     */
  1497. ;;*    declared correct.                                                */
  1498. ;;*---------------------------------------------------------------------*/
  1499. (defun flyspell-process-localwords (misspellings-buffer)
  1500.   (let (localwords case-fold-search
  1501.     (ispell-casechars (ispell-get-casechars)))
  1502.     ;; Get localwords from the original buffer
  1503.     (save-excursion
  1504.       (goto-char (point-min))
  1505.       ;; Localwords parsing copied from ispell.el.
  1506.       (while (search-forward ispell-words-keyword nil t)
  1507.     (let ((end (save-excursion (end-of-line) (point)))
  1508.           string)
  1509.       ;; buffer-local words separated by a space, and can contain
  1510.       ;; any character other than a space.  Not rigorous enough.
  1511.       (while (re-search-forward " *\\([^ ]+\\)" end t)
  1512.         (setq string (buffer-substring-no-properties (match-beginning 1)
  1513.                              (match-end 1)))
  1514.         ;; This can fail when string contains a word with invalid chars.
  1515.         ;; Error handling needs to be added between Ispell and Emacs.
  1516.         (if (and (< 1 (length string))
  1517.              (equal 0 (string-match ispell-casechars string)))
  1518.         (push string localwords))))))
  1519.     ;; Remove localwords matches from misspellings-buffer.
  1520.     ;; The usual mechanism of communicating the local words to ispell
  1521.     ;; does not affect the special ispell process used by
  1522.     ;; flyspell-large-region.
  1523.     (with-current-buffer misspellings-buffer
  1524.       (save-excursion
  1525.     (dolist (word localwords)
  1526.       (goto-char (point-min))
  1527.       (let ((regexp (concat "^" word "\n")))
  1528.         (while (re-search-forward regexp nil t)
  1529.           (delete-region (match-beginning 0) (match-end 0)))))))))
  1530.  
  1531. ;;* ---------------------------------------------------------------
  1532. ;;*     flyspell-check-region-doublons
  1533. ;;* ---------------------------------------------------------------
  1534. (defun flyspell-check-region-doublons (beg end)
  1535.   "Check for adjacent duplicated words (doublons) in the given region."
  1536.   (save-excursion
  1537.     (goto-char beg)
  1538.     (flyspell-word)     ; Make sure current word is checked
  1539.     (backward-word 1)
  1540.     (while (and (< (point) end)
  1541.         (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
  1542.                    end 'move))
  1543.       (flyspell-word)
  1544.       (backward-word 1))
  1545.     (flyspell-word)))
  1546.  
  1547. ;;*---------------------------------------------------------------------*/
  1548. ;;*    flyspell-large-region ...                                        */
  1549. ;;*---------------------------------------------------------------------*/
  1550. (defun flyspell-large-region (beg end)
  1551.   (let* ((curbuf  (current-buffer))
  1552.      (buffer  (get-buffer-create "*flyspell-region*")))
  1553.     (setq flyspell-external-ispell-buffer buffer)
  1554.     (setq flyspell-large-region-buffer curbuf)
  1555.     (setq flyspell-large-region-beg beg)
  1556.     (setq flyspell-large-region-end end)
  1557.     (flyspell-accept-buffer-local-defs)
  1558.     (set-buffer buffer)
  1559.     (erase-buffer)
  1560.     ;; this is done, we can start checking...
  1561.     (if flyspell-issue-message-flag (message "Checking region..."))
  1562.     (set-buffer curbuf)
  1563.     (ispell-set-spellchecker-params)  ; Initialize variables and dicts alists
  1564.     ;; Local dictionary becomes the global dictionary in use.
  1565.     (setq ispell-current-dictionary
  1566.       (or ispell-local-dictionary ispell-dictionary))
  1567.     (setq ispell-current-personal-dictionary
  1568.       (or ispell-local-pdict ispell-personal-dictionary))
  1569.     (let ((args (ispell-get-ispell-args))
  1570.       (encoding (ispell-get-coding-system))
  1571.       c)
  1572.       (if (and ispell-current-dictionary  ; use specified dictionary
  1573.            (not (member "-d" args)))  ; only define if not overridden
  1574.       (setq args
  1575.         (append (list "-d" ispell-current-dictionary) args)))
  1576.       (if ispell-current-personal-dictionary ; use specified pers dict
  1577.       (setq args
  1578.         (append args
  1579.             (list "-p"
  1580.                   (expand-file-name
  1581.                    ispell-current-personal-dictionary)))))
  1582.       (setq args (append args ispell-extra-args))
  1583.  
  1584.       ;; If we are using recent aspell or hunspell, make sure we use the right encoding
  1585.       ;; for communication. ispell or older aspell/hunspell does not support this
  1586.       (if ispell-encoding8-command
  1587.       (setq args
  1588.         (append args
  1589.             (list
  1590.              (concat ispell-encoding8-command
  1591.                  (symbol-name
  1592.                   encoding))))))
  1593.  
  1594.       (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
  1595.     (setq c (apply 'ispell-call-process-region beg
  1596.                end
  1597.                ispell-program-name
  1598.                nil
  1599.                buffer
  1600.                nil
  1601.                (if ispell-really-aspell "list" "-l")
  1602.                args)))
  1603.       (if (eq c 0)
  1604.       (progn
  1605.         (flyspell-process-localwords buffer)
  1606.         (with-current-buffer curbuf
  1607.           (flyspell-delete-region-overlays beg end)
  1608.           (flyspell-check-region-doublons beg end))
  1609.         (flyspell-external-point-words))
  1610.     (error "Can't check region...")))))
  1611.  
  1612. ;;*---------------------------------------------------------------------*/
  1613. ;;*    flyspell-region ...                                              */
  1614. ;;*    -------------------------------------------------------------    */
  1615. ;;*    Because `ispell -a' is too slow, it is not possible to use       */
  1616. ;;*    it on large region. Then, when ispell is invoked on a large      */
  1617. ;;*    text region, a new `ispell -l' process is spawned. The           */
  1618. ;;*    pointed out words are then searched in the region a checked with */
  1619. ;;*    regular flyspell means.                                          */
  1620. ;;*---------------------------------------------------------------------*/
  1621. ;;;###autoload
  1622. (defun flyspell-region (beg end)
  1623.   "Flyspell text between BEG and END."
  1624.   (interactive "r")
  1625.   (ispell-set-spellchecker-params)  ; Initialize variables and dicts alists
  1626.   (if (= beg end)
  1627.       ()
  1628.     (save-excursion
  1629.       (if (> beg end)
  1630.       (let ((old beg))
  1631.         (setq beg end)
  1632.         (setq end old)))
  1633.       (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
  1634.       (flyspell-large-region beg end)
  1635.     (flyspell-small-region beg end)))))
  1636.  
  1637. ;;*---------------------------------------------------------------------*/
  1638. ;;*    flyspell-buffer ...                                              */
  1639. ;;*---------------------------------------------------------------------*/
  1640. ;;;###autoload
  1641. (defun flyspell-buffer ()
  1642.   "Flyspell whole buffer."
  1643.   (interactive)
  1644.   (flyspell-region (point-min) (point-max)))
  1645.  
  1646. ;;*---------------------------------------------------------------------*/
  1647. ;;*    old next error position ...                                      */
  1648. ;;*---------------------------------------------------------------------*/
  1649. (defvar flyspell-old-buffer-error nil)
  1650. (defvar flyspell-old-pos-error nil)
  1651.  
  1652. ;;*---------------------------------------------------------------------*/
  1653. ;;*    flyspell-goto-next-error ...                                     */
  1654. ;;*---------------------------------------------------------------------*/
  1655. (defun flyspell-goto-next-error ()
  1656.   "Go to the next previously detected error.
  1657. In general FLYSPELL-GOTO-NEXT-ERROR must be used after
  1658. FLYSPELL-BUFFER."
  1659.   (interactive)
  1660.   (let ((pos (point))
  1661.     (max (point-max)))
  1662.     (if (and (eq (current-buffer) flyspell-old-buffer-error)
  1663.          (eq pos flyspell-old-pos-error))
  1664.     (progn
  1665.       (if (= flyspell-old-pos-error max)
  1666.           ;; goto beginning of buffer
  1667.           (progn
  1668.         (message "Restarting from beginning of buffer")
  1669.         (goto-char (point-min)))
  1670.         (forward-word 1))
  1671.       (setq pos (point))))
  1672.     ;; seek the next error
  1673.     (while (and (< pos max)
  1674.         (let ((ovs (overlays-at pos))
  1675.               (r '()))
  1676.           (while (and (not r) (consp ovs))
  1677.             (if (flyspell-overlay-p (car ovs))
  1678.             (setq r t)
  1679.               (setq ovs (cdr ovs))))
  1680.           (not r)))
  1681.       (setq pos (1+ pos)))
  1682.     ;; save the current location for next invocation
  1683.     (setq flyspell-old-pos-error pos)
  1684.     (setq flyspell-old-buffer-error (current-buffer))
  1685.     (goto-char pos)
  1686.     (if (= pos max)
  1687.     (message "No more miss-spelled word!"))))
  1688.  
  1689. ;;*---------------------------------------------------------------------*/
  1690. ;;*    flyspell-overlay-p ...                                           */
  1691. ;;*---------------------------------------------------------------------*/
  1692. (defun flyspell-overlay-p (o)
  1693.   "Return true if O is an overlay used by flyspell."
  1694.   (and (overlayp o) (overlay-get o 'flyspell-overlay)))
  1695.  
  1696. ;;*---------------------------------------------------------------------*/
  1697. ;;*    flyspell-delete-region-overlays, flyspell-delete-all-overlays    */
  1698. ;;*    -------------------------------------------------------------    */
  1699. ;;*    Remove overlays introduced by flyspell.                          */
  1700. ;;*---------------------------------------------------------------------*/
  1701. (defun flyspell-delete-region-overlays (beg end)
  1702.   "Delete overlays used by flyspell in a given region."
  1703.   (if (fboundp 'remove-overlays)
  1704.       (remove-overlays beg end 'flyspell-overlay t)
  1705.     (let ((l (overlays-in beg end)))
  1706.       (while (consp l)
  1707.     (progn
  1708.       (if (flyspell-overlay-p (car l))
  1709.           (delete-overlay (car l)))
  1710.       (setq l (cdr l)))))))
  1711.  
  1712. (defun flyspell-delete-all-overlays ()
  1713.   "Delete all the overlays used by flyspell."
  1714.   (if (fboundp 'remove-overlays)
  1715.       (remove-overlays (point-min) (point-max) 'flyspell-overlay t)
  1716.     (flyspell-delete-region-overlays (point-min) (point-max))))
  1717.  
  1718. ;;*---------------------------------------------------------------------*/
  1719. ;;*    flyspell-unhighlight-at ...                                      */
  1720. ;;*---------------------------------------------------------------------*/
  1721. (defun flyspell-unhighlight-at (pos)
  1722.   "Remove the flyspell overlay that are located at POS."
  1723.   (if flyspell-persistent-highlight
  1724.       (let ((overlays (overlays-at pos)))
  1725.     (while (consp overlays)
  1726.       (if (flyspell-overlay-p (car overlays))
  1727.           (delete-overlay (car overlays)))
  1728.       (setq overlays (cdr overlays))))
  1729.     (if (flyspell-overlay-p flyspell-overlay)
  1730.         (delete-overlay flyspell-overlay))))
  1731.  
  1732. ;;*---------------------------------------------------------------------*/
  1733. ;;*    flyspell-properties-at-p ...                                     */
  1734. ;;*    -------------------------------------------------------------    */
  1735. ;;*    Is there an highlight properties at position pos?                */
  1736. ;;*---------------------------------------------------------------------*/
  1737. (defun flyspell-properties-at-p (pos)
  1738.   "Return t if there is a text property at POS, not counting `local-map'.
  1739. If variable `flyspell-highlight-properties' is set to nil,
  1740. text with properties are not checked.  This function is used to discover
  1741. if the character at POS has any other property."
  1742.   (let ((prop (text-properties-at pos))
  1743.     (keep t))
  1744.     (while (and keep (consp prop))
  1745.       (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
  1746.       (setq prop (cdr (cdr prop)))
  1747.     (setq keep nil)))
  1748.     (consp prop)))
  1749.  
  1750. ;;*---------------------------------------------------------------------*/
  1751. ;;*    make-flyspell-overlay ...                                        */
  1752. ;;*---------------------------------------------------------------------*/
  1753. (defun make-flyspell-overlay (beg end face mouse-face)
  1754.   "Allocate an overlay to highlight an incorrect word.
  1755. BEG and END specify the range in the buffer of that word.
  1756. FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
  1757. for the overlay."
  1758.   (let ((overlay (make-overlay beg end nil t nil)))
  1759.     (overlay-put overlay 'face face)
  1760.     (overlay-put overlay 'mouse-face mouse-face)
  1761.     (overlay-put overlay 'flyspell-overlay t)
  1762.     (overlay-put overlay 'evaporate t)
  1763.     (overlay-put overlay 'help-echo "mouse-2: correct word at point")
  1764.     (overlay-put overlay 'keymap flyspell-mouse-map)
  1765.     (when (eq face 'flyspell-incorrect)
  1766.       (and (stringp flyspell-before-incorrect-word-string)
  1767.            (overlay-put overlay 'before-string
  1768.                         flyspell-before-incorrect-word-string))
  1769.       (and (stringp flyspell-after-incorrect-word-string)
  1770.            (overlay-put overlay 'after-string
  1771.                         flyspell-after-incorrect-word-string)))
  1772.     overlay))
  1773.  
  1774. ;;*---------------------------------------------------------------------*/
  1775. ;;*    flyspell-highlight-incorrect-region ...                          */
  1776. ;;*---------------------------------------------------------------------*/
  1777. (defun flyspell-highlight-incorrect-region (beg end poss)
  1778.   "Set up an overlay on a misspelled word, in the buffer from BEG to END.
  1779. POSS is usually a list of possible spelling/correction lists,
  1780. as returned by `ispell-parse-output'.
  1781. It can also be the symbol `doublon', in the case where the word
  1782. is itself incorrect, but suspiciously repeated."
  1783.   (let ((inhibit-read-only t))
  1784.     (unless (run-hook-with-args-until-success
  1785.          'flyspell-incorrect-hook beg end poss)
  1786.       (if (or flyspell-highlight-properties
  1787.           (not (flyspell-properties-at-p beg)))
  1788.       (progn
  1789.         ;; we cleanup all the overlay that are in the region, not
  1790.         ;; beginning at the word start position
  1791.         (if (< (1+ beg) end)
  1792.         (let ((os (overlays-in (1+ beg) end)))
  1793.           (while (consp os)
  1794.             (if (flyspell-overlay-p (car os))
  1795.             (delete-overlay (car os)))
  1796.             (setq os (cdr os)))))
  1797.         ;; we cleanup current overlay at the same position
  1798.             (flyspell-unhighlight-at beg)
  1799.         ;; now we can use a new overlay
  1800.         (setq flyspell-overlay
  1801.           (make-flyspell-overlay
  1802.            beg end
  1803.            (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
  1804.            'highlight)))))))
  1805.  
  1806. ;;*---------------------------------------------------------------------*/
  1807. ;;*    flyspell-highlight-duplicate-region ...                          */
  1808. ;;*---------------------------------------------------------------------*/
  1809. (defun flyspell-highlight-duplicate-region (beg end poss)
  1810.   "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
  1811. POSS is a list of possible spelling/correction lists,
  1812. as returned by `ispell-parse-output'."
  1813.   (let ((inhibit-read-only t))
  1814.     (unless (run-hook-with-args-until-success
  1815.          'flyspell-incorrect-hook beg end poss)
  1816.       (if (or flyspell-highlight-properties
  1817.           (not (flyspell-properties-at-p beg)))
  1818.       (progn
  1819.         ;; we cleanup current overlay at the same position
  1820.             (flyspell-unhighlight-at beg)
  1821.         ;; now we can use a new overlay
  1822.         (setq flyspell-overlay
  1823.           (make-flyspell-overlay beg end
  1824.                      'flyspell-duplicate
  1825.                      'highlight)))))))
  1826.  
  1827. ;;*---------------------------------------------------------------------*/
  1828. ;;*    flyspell-auto-correct-cache ...                                  */
  1829. ;;*---------------------------------------------------------------------*/
  1830. (defvar flyspell-auto-correct-pos nil)
  1831. (defvar flyspell-auto-correct-region nil)
  1832. (defvar flyspell-auto-correct-ring nil)
  1833. (defvar flyspell-auto-correct-word nil)
  1834. (make-variable-buffer-local 'flyspell-auto-correct-pos)
  1835. (make-variable-buffer-local 'flyspell-auto-correct-region)
  1836. (make-variable-buffer-local 'flyspell-auto-correct-ring)
  1837. (make-variable-buffer-local 'flyspell-auto-correct-word)
  1838.  
  1839. ;;*---------------------------------------------------------------------*/
  1840. ;;*    flyspell-check-previous-highlighted-word ...                     */
  1841. ;;*---------------------------------------------------------------------*/
  1842. (defun flyspell-check-previous-highlighted-word (&optional arg)
  1843.   "Correct the closer misspelled word.
  1844. This function scans a mis-spelled word before the cursor. If it finds one
  1845. it proposes replacement for that word. With prefix arg, count that many
  1846. misspelled words backwards."
  1847.   (interactive)
  1848.   (let ((pos1 (point))
  1849.     (pos  (point))
  1850.     (arg  (if (or (not (numberp arg)) (< arg 1)) 1 arg))
  1851.     ov ovs)
  1852.     (if (catch 'exit
  1853.       (while (and (setq pos (previous-overlay-change pos))
  1854.               (not (= pos pos1)))
  1855.         (setq pos1 pos)
  1856.         (if (> pos (point-min))
  1857.         (progn
  1858.           (setq ovs (overlays-at (1- pos)))
  1859.           (while (consp ovs)
  1860.             (setq ov (car ovs))
  1861.             (setq ovs (cdr ovs))
  1862.             (if (and (flyspell-overlay-p ov)
  1863.                  (= 0 (setq arg (1- arg))))
  1864.             (throw 'exit t)))))))
  1865.     (save-excursion
  1866.       (goto-char pos)
  1867.       (ispell-word))
  1868.       (error "No word to correct before point"))))
  1869.  
  1870. ;;*---------------------------------------------------------------------*/
  1871. ;;*    flyspell-display-next-corrections ...                            */
  1872. ;;*---------------------------------------------------------------------*/
  1873. (defun flyspell-display-next-corrections (corrections)
  1874.   (let ((string "Corrections:")
  1875.     (l corrections)
  1876.     (pos '()))
  1877.     (while (< (length string) 80)
  1878.       (if (equal (car l) flyspell-auto-correct-word)
  1879.       (setq pos (cons (+ 1 (length string)) pos)))
  1880.       (setq string (concat string " " (car l)))
  1881.       (setq l (cdr l)))
  1882.     (while (consp pos)
  1883.       (let ((num (car pos)))
  1884.     (put-text-property num
  1885.                (+ num (length flyspell-auto-correct-word))
  1886.                'face 'flyspell-incorrect
  1887.                string))
  1888.       (setq pos (cdr pos)))
  1889.     (if (fboundp 'display-message)
  1890.     (display-message 'no-log string)
  1891.       (message "%s" string))))
  1892.  
  1893. ;;*---------------------------------------------------------------------*/
  1894. ;;*    flyspell-abbrev-table ...                                        */
  1895. ;;*---------------------------------------------------------------------*/
  1896. (defun flyspell-abbrev-table ()
  1897.   (if flyspell-use-global-abbrev-table-p
  1898.       global-abbrev-table
  1899.     (or local-abbrev-table global-abbrev-table)))
  1900.  
  1901. ;;*---------------------------------------------------------------------*/
  1902. ;;*    flyspell-define-abbrev ...                                       */
  1903. ;;*---------------------------------------------------------------------*/
  1904. (defun flyspell-define-abbrev (name expansion)
  1905.   (let ((table (flyspell-abbrev-table)))
  1906.     (when table
  1907.       (define-abbrev table (downcase name) expansion))))
  1908.  
  1909. ;;*---------------------------------------------------------------------*/
  1910. ;;*    flyspell-auto-correct-word ...                                   */
  1911. ;;*---------------------------------------------------------------------*/
  1912. (defun flyspell-auto-correct-word ()
  1913.   "Correct the current word.
  1914. This command proposes various successive corrections for the current word."
  1915.   (interactive)
  1916.   (let ((pos     (point))
  1917.     (old-max (point-max)))
  1918.     ;; use the correct dictionary
  1919.     (flyspell-accept-buffer-local-defs)
  1920.     (if (and (eq flyspell-auto-correct-pos pos)
  1921.          (consp flyspell-auto-correct-region))
  1922.     ;; we have already been using the function at the same location
  1923.     (let* ((start (car flyspell-auto-correct-region))
  1924.            (len   (cdr flyspell-auto-correct-region)))
  1925.       (flyspell-unhighlight-at start)
  1926.       (delete-region start (+ start len))
  1927.       (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
  1928.       (let* ((word (car flyspell-auto-correct-ring))
  1929.          (len  (length word)))
  1930.         (rplacd flyspell-auto-correct-region len)
  1931.         (goto-char start)
  1932.         (if flyspell-abbrev-p
  1933.         (if (flyspell-already-abbrevp (flyspell-abbrev-table)
  1934.                           flyspell-auto-correct-word)
  1935.             (flyspell-change-abbrev (flyspell-abbrev-table)
  1936.                         flyspell-auto-correct-word
  1937.                         word)
  1938.           (flyspell-define-abbrev flyspell-auto-correct-word word)))
  1939.         (funcall flyspell-insert-function word)
  1940.         (flyspell-word)
  1941.         (flyspell-display-next-corrections flyspell-auto-correct-ring))
  1942.       (flyspell-ajust-cursor-point pos (point) old-max)
  1943.       (setq flyspell-auto-correct-pos (point)))
  1944.       ;; fetch the word to be checked
  1945.       (let ((word (flyspell-get-word nil)))
  1946.     (if (consp word)
  1947.         (let ((start (car (cdr word)))
  1948.           (end (car (cdr (cdr word))))
  1949.           (word (car word))
  1950.           poss ispell-filter)
  1951.           (setq flyspell-auto-correct-word word)
  1952.           ;; now check spelling of word.
  1953.           (ispell-send-string "%\n") ;put in verbose mode
  1954.           (ispell-send-string (concat "^" word "\n"))
  1955.               ;; wait until ispell has processed word.
  1956.               (while (progn
  1957.                        (accept-process-output ispell-process)
  1958.                        (not (string= "" (car ispell-filter)))))
  1959.           ;; Remove leading empty element
  1960.           (setq ispell-filter (cdr ispell-filter))
  1961.           ;; ispell process should return something after word is sent.
  1962.           ;; Tag word as valid (i.e., skip) otherwise
  1963.           (or ispell-filter
  1964.           (setq ispell-filter '(*)))
  1965.           (if (consp ispell-filter)
  1966.           (setq poss (ispell-parse-output (car ispell-filter))))
  1967.           (cond
  1968.            ((or (eq poss t) (stringp poss))
  1969.         ;; don't correct word
  1970.         t)
  1971.            ((null poss)
  1972.         ;; ispell error
  1973.         (error "Ispell: error in Ispell process"))
  1974.            (t
  1975.         ;; the word is incorrect, we have to propose a replacement
  1976.         (let ((replacements (if flyspell-sort-corrections
  1977.                     (sort (car (cdr (cdr poss))) 'string<)
  1978.                       (car (cdr (cdr poss))))))
  1979.           (setq flyspell-auto-correct-region nil)
  1980.           (if (consp replacements)
  1981.               (progn
  1982.             (let ((replace (car replacements)))
  1983.               (let ((new-word replace))
  1984.                 (if (not (equal new-word (car poss)))
  1985.                 (progn
  1986.                   ;; the save the current replacements
  1987.                   (setq flyspell-auto-correct-region
  1988.                     (cons start (length new-word)))
  1989.                   (let ((l replacements))
  1990.                     (while (consp (cdr l))
  1991.                       (setq l (cdr l)))
  1992.                     (rplacd l (cons (car poss) replacements)))
  1993.                   (setq flyspell-auto-correct-ring
  1994.                     replacements)
  1995.                   (flyspell-unhighlight-at start)
  1996.                   (delete-region start end)
  1997.                   (funcall flyspell-insert-function new-word)
  1998.                   (if flyspell-abbrev-p
  1999.                       (if (flyspell-already-abbrevp
  2000.                        (flyspell-abbrev-table) word)
  2001.                       (flyspell-change-abbrev
  2002.                        (flyspell-abbrev-table)
  2003.                        word
  2004.                        new-word)
  2005.                     (flyspell-define-abbrev word
  2006.                                 new-word)))
  2007.                   (flyspell-word)
  2008.                   (flyspell-display-next-corrections
  2009.                    (cons new-word flyspell-auto-correct-ring))
  2010.                   (flyspell-ajust-cursor-point pos
  2011.                                    (point)
  2012.                                    old-max))))))))))
  2013.           (setq flyspell-auto-correct-pos (point))
  2014.           (ispell-pdict-save t)))))))
  2015.  
  2016. ;;*---------------------------------------------------------------------*/
  2017. ;;*    flyspell-auto-correct-previous-pos ...                           */
  2018. ;;*---------------------------------------------------------------------*/
  2019. (defvar flyspell-auto-correct-previous-pos nil
  2020.   "Holds the start of the first incorrect word before point.")
  2021.  
  2022. ;;*---------------------------------------------------------------------*/
  2023. ;;*    flyspell-auto-correct-previous-hook ...                          */
  2024. ;;*---------------------------------------------------------------------*/
  2025. (defun flyspell-auto-correct-previous-hook ()
  2026.   "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
  2027. Sets `flyspell-auto-correct-previous-pos' to nil"
  2028.   (interactive)
  2029.   (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
  2030.   (unless (eq this-command (function flyspell-auto-correct-previous-word))
  2031.     (setq flyspell-auto-correct-previous-pos nil)))
  2032.  
  2033. ;;*---------------------------------------------------------------------*/
  2034. ;;*    flyspell-auto-correct-previous-word ...                          */
  2035. ;;*---------------------------------------------------------------------*/
  2036. (defun flyspell-auto-correct-previous-word (position)
  2037.   "Auto correct the first mispelled word that occurs before point.
  2038. But don't look beyond what's visible on the screen."
  2039.   (interactive "d")
  2040.  
  2041.   (let ((top (window-start))
  2042.     (bot (window-end)))
  2043.     (save-excursion
  2044.       (save-restriction
  2045.     (narrow-to-region top bot)
  2046.     (overlay-recenter (point))
  2047.  
  2048.     (add-hook 'pre-command-hook
  2049.           (function flyspell-auto-correct-previous-hook) t t)
  2050.  
  2051.     (unless flyspell-auto-correct-previous-pos
  2052.       ;; only reset if a new overlay exists
  2053.       (setq flyspell-auto-correct-previous-pos nil)
  2054.  
  2055.       (let ((overlay-list (overlays-in (point-min) position))
  2056.         (new-overlay 'dummy-value))
  2057.  
  2058.         ;; search for previous (new) flyspell overlay
  2059.         (while (and new-overlay
  2060.             (or (not (flyspell-overlay-p new-overlay))
  2061.                 ;; check if its face has changed
  2062.                 (not (eq (get-char-property
  2063.                       (overlay-start new-overlay) 'face)
  2064.                      'flyspell-incorrect))))
  2065.           (setq new-overlay (car-safe overlay-list))
  2066.           (setq overlay-list (cdr-safe overlay-list)))
  2067.  
  2068.         ;; if nothing new exits new-overlay should be nil
  2069.         (if new-overlay ;; the length of the word may change so go to the start
  2070.         (setq flyspell-auto-correct-previous-pos
  2071.               (overlay-start new-overlay)))))
  2072.  
  2073.     (when flyspell-auto-correct-previous-pos
  2074.       (save-excursion
  2075.         (goto-char flyspell-auto-correct-previous-pos)
  2076.         (let ((ispell-following-word t)) ;; point is at start
  2077.           (if (numberp flyspell-auto-correct-previous-pos)
  2078.           (goto-char flyspell-auto-correct-previous-pos))
  2079.           (flyspell-auto-correct-word))
  2080.         ;; the point may have moved so reset this
  2081.         (setq flyspell-auto-correct-previous-pos (point))))))))
  2082.  
  2083. ;;*---------------------------------------------------------------------*/
  2084. ;;*    flyspell-correct-word ...                                        */
  2085. ;;*---------------------------------------------------------------------*/
  2086.  
  2087. (defun flyspell-correct-word (event)
  2088.   "Pop up a menu of possible corrections for a misspelled word.
  2089. The word checked is the word at the mouse position."
  2090.   (interactive "e")
  2091.   (let ((save (point)))
  2092.     (mouse-set-point event)
  2093.     (flyspell-correct-word-before-point event save)))
  2094.  
  2095. (defun flyspell-correct-word-before-point (&optional event opoint)
  2096.   "Pop up a menu of possible corrections for misspelled word before point.
  2097. If EVENT is non-nil, it is the mouse event that invoked this operation;
  2098. that controls where to put the menu.
  2099. If OPOINT is non-nil, restore point there after adjusting it for replacement."
  2100.   (interactive)
  2101.   (unless (mouse-position)
  2102.     (error "Pop-up menus do not work on this terminal"))
  2103.   ;; use the correct dictionary
  2104.   (flyspell-accept-buffer-local-defs)
  2105.   (or opoint (setq opoint (point-marker)))
  2106.   (let ((cursor-location (point))
  2107.     (word (flyspell-get-word nil)))
  2108.     (if (consp word)
  2109.     (let ((start (car (cdr word)))
  2110.           (end (car (cdr (cdr word))))
  2111.           (word (car word))
  2112.           poss ispell-filter)
  2113.       ;; now check spelling of word.
  2114.       (ispell-send-string "%\n")    ;put in verbose mode
  2115.       (ispell-send-string (concat "^" word "\n"))
  2116.       ;; wait until ispell has processed word
  2117.       (while (progn
  2118.            (accept-process-output ispell-process)
  2119.            (not (string= "" (car ispell-filter)))))
  2120.       ;; Remove leading empty element
  2121.       (setq ispell-filter (cdr ispell-filter))
  2122.       ;; ispell process should return something after word is sent.
  2123.       ;; Tag word as valid (i.e., skip) otherwise
  2124.       (or ispell-filter
  2125.           (setq ispell-filter '(*)))
  2126.       (if (consp ispell-filter)
  2127.           (setq poss (ispell-parse-output (car ispell-filter))))
  2128.       (cond
  2129.        ((or (eq poss t) (stringp poss))
  2130.         ;; don't correct word
  2131.         t)
  2132.        ((null poss)
  2133.         ;; ispell error
  2134.         (error "Ispell: error in Ispell process"))
  2135.        ((featurep 'xemacs)
  2136.         (flyspell-xemacs-popup
  2137.          poss word cursor-location start end opoint))
  2138.        (t
  2139.         ;; The word is incorrect, we have to propose a replacement.
  2140.         (flyspell-do-correct (flyspell-emacs-popup event poss word)
  2141.                  poss word cursor-location start end opoint)))
  2142.       (ispell-pdict-save t)))))
  2143.  
  2144. ;;*---------------------------------------------------------------------*/
  2145. ;;*    flyspell-do-correct ...                                      */
  2146. ;;*---------------------------------------------------------------------*/
  2147. (defun flyspell-do-correct (replace poss word cursor-location start end save)
  2148.   "The popup menu callback."
  2149.   ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
  2150.   ;; it instead right after calling the function.
  2151.   (cond ((eq replace 'ignore)
  2152.          (goto-char save)
  2153.      nil)
  2154.     ((eq replace 'save)
  2155.          (goto-char save)
  2156.      (ispell-send-string (concat "*" word "\n"))
  2157.          ;; This was added only to the XEmacs side in revision 1.18 of
  2158.          ;; flyspell.  I assume its absence on the Emacs side was an
  2159.          ;; oversight.  --Stef
  2160.      (ispell-send-string "#\n")
  2161.      (flyspell-unhighlight-at cursor-location)
  2162.      (setq ispell-pdict-modified-p '(t)))
  2163.     ((or (eq replace 'buffer) (eq replace 'session))
  2164.      (ispell-send-string (concat "@" word "\n"))
  2165.      (flyspell-unhighlight-at cursor-location)
  2166.      (if (null ispell-pdict-modified-p)
  2167.          (setq ispell-pdict-modified-p
  2168.            (list ispell-pdict-modified-p)))
  2169.          (goto-char save)
  2170.      (if (eq replace 'buffer)
  2171.          (ispell-add-per-file-word-list word)))
  2172.     (replace
  2173.          ;; This was added only to the Emacs side.  I assume its absence on
  2174.          ;; the XEmacs side was an oversight.  --Stef
  2175.          (flyspell-unhighlight-at cursor-location)
  2176.      (let ((old-max (point-max))
  2177.            (new-word (if (atom replace)
  2178.                  replace
  2179.                (car replace)))
  2180.            (cursor-location (+ (- (length word) (- end start))
  2181.                    cursor-location)))
  2182.        (unless (equal new-word (car poss))
  2183.              (delete-region start end)
  2184.              (goto-char start)
  2185.              (funcall flyspell-insert-function new-word)
  2186.              (if flyspell-abbrev-p
  2187.                  (flyspell-define-abbrev word new-word)))
  2188.            ;; In the original Emacs code, this was only called in the body
  2189.            ;; of the if.  I arbitrarily kept the XEmacs behavior instead.
  2190.            (flyspell-ajust-cursor-point save cursor-location old-max)))
  2191.         (t
  2192.          (goto-char save)
  2193.          nil)))
  2194.  
  2195. ;;*---------------------------------------------------------------------*/
  2196. ;;*    flyspell-ajust-cursor-point ...                                  */
  2197. ;;*---------------------------------------------------------------------*/
  2198. (defun flyspell-ajust-cursor-point (save cursor-location old-max)
  2199.   (if (>= save cursor-location)
  2200.       (let ((new-pos (+ save (- (point-max) old-max))))
  2201.     (goto-char (cond
  2202.             ((< new-pos (point-min))
  2203.              (point-min))
  2204.             ((> new-pos (point-max))
  2205.              (point-max))
  2206.             (t new-pos))))
  2207.     (goto-char save)))
  2208.  
  2209. ;;*---------------------------------------------------------------------*/
  2210. ;;*    flyspell-emacs-popup ...                                         */
  2211. ;;*---------------------------------------------------------------------*/
  2212. (defun flyspell-emacs-popup (event poss word)
  2213.   "The Emacs popup menu."
  2214.   (unless window-system
  2215.     (error "This command requires pop-up dialogs"))
  2216.   (if (not event)
  2217.       (let* ((mouse-pos  (mouse-position))
  2218.          (mouse-pos  (if (nth 1 mouse-pos)
  2219.                  mouse-pos
  2220.                (set-mouse-position (car mouse-pos)
  2221.                             (/ (frame-width) 2) 2)
  2222.                (mouse-position))))
  2223.     (setq event (list (list (car (cdr mouse-pos))
  2224.                 (1+ (cdr (cdr mouse-pos))))
  2225.               (car mouse-pos)))))
  2226.   (let* ((corrects   (if flyspell-sort-corrections
  2227.              (sort (car (cdr (cdr poss))) 'string<)
  2228.                (car (cdr (cdr poss)))))
  2229.      (cor-menu   (if (consp corrects)
  2230.              (mapcar (lambda (correct)
  2231.                    (list correct correct))
  2232.                  corrects)
  2233.                '()))
  2234.      (affix      (car (cdr (cdr (cdr poss)))))
  2235.      show-affix-info
  2236.      (base-menu  (let ((save (if (and (consp affix) show-affix-info)
  2237.                      (list
  2238.                       (list (concat "Save affix: " (car affix))
  2239.                         'save)
  2240.                       '("Accept (session)" session)
  2241.                       '("Accept (buffer)" buffer))
  2242.                    '(("Save word" save)
  2243.                      ("Accept (session)" session)
  2244.                      ("Accept (buffer)" buffer)))))
  2245.                (if (consp cor-menu)
  2246.                (append cor-menu (cons "" save))
  2247.              save)))
  2248.      (menu       (cons "flyspell correction menu" base-menu)))
  2249.     (car (x-popup-menu event
  2250.                (list (format "%s [%s]" word (or ispell-local-dictionary
  2251.                             ispell-dictionary))
  2252.                  menu)))))
  2253.  
  2254. ;;*---------------------------------------------------------------------*/
  2255. ;;*    flyspell-xemacs-popup ...                                        */
  2256. ;;*---------------------------------------------------------------------*/
  2257. (defun flyspell-xemacs-popup (poss word cursor-location start end save)
  2258.   "The XEmacs popup menu."
  2259.   (let* ((corrects   (if flyspell-sort-corrections
  2260.              (sort (car (cdr (cdr poss))) 'string<)
  2261.                (car (cdr (cdr poss)))))
  2262.      (cor-menu   (if (consp corrects)
  2263.              (mapcar (lambda (correct)
  2264.                    (vector correct
  2265.                        (list 'flyspell-do-correct
  2266.                          correct
  2267.                          (list 'quote poss)
  2268.                          word
  2269.                          cursor-location
  2270.                          start
  2271.                          end
  2272.                          save)
  2273.                        t))
  2274.                  corrects)
  2275.                '()))
  2276.      (affix      (car (cdr (cdr (cdr poss)))))
  2277.      show-affix-info
  2278.      (menu       (let ((save (if (and (consp affix) show-affix-info)
  2279.                      (vector
  2280.                       (concat "Save affix: " (car affix))
  2281.                       (list 'flyspell-do-correct
  2282.                         ''save
  2283.                         (list 'quote poss)
  2284.                         word
  2285.                         cursor-location
  2286.                         start
  2287.                         end
  2288.                         save)
  2289.                       t)
  2290.                    (vector
  2291.                     "Save word"
  2292.                     (list 'flyspell-do-correct
  2293.                       ''save
  2294.                       (list 'quote poss)
  2295.                       word
  2296.                       cursor-location
  2297.                       start
  2298.                       end
  2299.                       save)
  2300.                     t)))
  2301.                (session (vector "Accept (session)"
  2302.                         (list 'flyspell-do-correct
  2303.                           ''session
  2304.                           (list 'quote poss)
  2305.                           word
  2306.                           cursor-location
  2307.                           start
  2308.                           end
  2309.                           save)
  2310.                         t))
  2311.                (buffer  (vector "Accept (buffer)"
  2312.                         (list 'flyspell-do-correct
  2313.                           ''buffer
  2314.                           (list 'quote poss)
  2315.                           word
  2316.                           cursor-location
  2317.                           start
  2318.                           end
  2319.                           save)
  2320.                         t)))
  2321.                (if (consp cor-menu)
  2322.                (append cor-menu (list "-" save session buffer))
  2323.              (list save session buffer)))))
  2324.     (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
  2325.                          ispell-dictionary))
  2326.               menu))))
  2327.  
  2328. ;;*---------------------------------------------------------------------*/
  2329. ;;*    Some example functions for real autocorrecting                   */
  2330. ;;*---------------------------------------------------------------------*/
  2331. (defun flyspell-maybe-correct-transposition (beg end poss)
  2332.   "Check replacements for transposed characters.
  2333.  
  2334. If the text between BEG and END is equal to a correction suggested by
  2335. Ispell, after transposing two adjacent characters, correct the text,
  2336. and return t.
  2337.  
  2338. The third arg POSS is either the symbol 'doublon' or a list of
  2339. possible corrections as returned by `ispell-parse-output'.
  2340.  
  2341. This function is meant to be added to `flyspell-incorrect-hook'."
  2342.   (when (consp poss)
  2343.     (catch 'done
  2344.       (let ((str (buffer-substring beg end))
  2345.         (i 0) (len (- end beg)) tmp)
  2346.     (while (< (1+ i) len)
  2347.       (setq tmp (aref str i))
  2348.       (aset str i (aref str (1+ i)))
  2349.       (aset str (1+ i) tmp)
  2350.           (when (member str (nth 2 poss))
  2351.         (save-excursion
  2352.           (goto-char (+ beg i 1))
  2353.           (transpose-chars 1))
  2354.         (throw 'done t))
  2355.       (setq tmp (aref str i))
  2356.       (aset str i (aref str (1+ i)))
  2357.       (aset str (1+ i) tmp)
  2358.       (setq i (1+ i))))
  2359.       nil)))
  2360.  
  2361. (defun flyspell-maybe-correct-doubling (beg end poss)
  2362.   "Check replacements for doubled characters.
  2363.  
  2364. If the text between BEG and END is equal to a correction suggested by
  2365. Ispell, after removing a pair of doubled characters, correct the text,
  2366. and return t.
  2367.  
  2368. The third arg POSS is either the symbol 'doublon' or a list of
  2369. possible corrections as returned by `ispell-parse-output'.
  2370.  
  2371. This function is meant to be added to `flyspell-incorrect-hook'."
  2372.   (when (consp poss)
  2373.     (catch 'done
  2374.       (let ((str (buffer-substring beg end))
  2375.         (i 0) (len (- end beg)))
  2376.     (while (< (1+ i) len)
  2377.       (when (and (= (aref str i) (aref str (1+ i)))
  2378.              (member (concat (substring str 0 (1+ i))
  2379.                      (substring str (+ i 2)))
  2380.                  (nth 2 poss)))
  2381.         (goto-char (+ beg i))
  2382.         (delete-char 1)
  2383.         (throw 'done t))
  2384.       (setq i (1+ i))))
  2385.       nil)))
  2386.  
  2387. ;;*---------------------------------------------------------------------*/
  2388. ;;*    flyspell-already-abbrevp ...                                     */
  2389. ;;*---------------------------------------------------------------------*/
  2390. (defun flyspell-already-abbrevp (table word)
  2391.   (let ((sym (abbrev-symbol word table)))
  2392.     (and sym (symbolp sym))))
  2393.  
  2394. ;;*---------------------------------------------------------------------*/
  2395. ;;*    flyspell-change-abbrev ...                                       */
  2396. ;;*---------------------------------------------------------------------*/
  2397. (defun flyspell-change-abbrev (table old new)
  2398.   (set (abbrev-symbol old table) new))
  2399.  
  2400. (provide 'flyspell)
  2401.  
  2402. ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
  2403. ;;; flyspell.el ends here
  2404.